# Nim macros I confess that Nim macros defeated me. My expectations were that I 
would learn Nim macros as easily as I had learned Lisp macros, but no, Nim 
macros are tough. Then I started to create theories about my difficulty in 
learning Nim macros.

## Theory 1: Lisp chauvinism Nim macros are easy, but I am so used to Lisp 
macros that I cannot see the Nim solution. It is easy to test this theory: If I 
present the problem of learning Nim and Lisp to young programmers, they will 
learn Nim macros as easily as Lisp macros.

In fact, since I am a professor, it was easy to perform an experiment -- I have 
presented the challenge of learning Lisp macros and Nim macros to students of 
universities, where I have good contacts. The subjects of this experiment 
already knew Python, and Nim has a Python like syntax, therefore, I believed 
that the students would consider Nim much easier than Lisp.

I gave the students the choice of three books from which to learn Lisp, _On 
Lisp_ , by Paul Graham, _Let Over Lambda_ , by Doug Hoyte, and _Land of Lisp_ , 
by Conrad Barski.

After two weeks, most of the students were creating Lisp macros for lazy 
evaluation, loops of different kinds, reverse polish notation, and so on. 
However, the students had more difficulty with Nim macros than myself.

Therefore, I dropped the theory that I was a Lisp chauvinist, and created 
another one...

## Theory 2: Great books Lisp has great books in Chinese, English, French and 
Russian that are distributed for free throughout the Internet. Many students 
told me that studying Lisp from a book written by Svyatoslav Sergeevich Lavrov 
was inspiring, since he is the very man, who was responsible for the 
calculation of the trajectories of so many spaceships at the dawn of 
astronautics, such as Sputnik 1, Vostok 1 with Yuri Gagarin, the Venera probes, 
Luna 3, etc.

Besides the great book by Lavrov, _On Lisp_ and _Let Over Lambda_ deal almost 
exclusively with macros. The environment that the students use to learn Lisp, I 
mean PTC, is very exciting too. The PTC CAD/CAM program seems like Jarvis, the 
butler from Iron Man, and you can use it to design screws, gear cogs and whole 
machines, if you are smart enough.

To test this second theory, I decided to write a book about Nim macros, and see 
whether the students could learn from my book. The problem is that I don't 
really know Nim macros that well, and my experience in programming is not as 
inspiring, as is the case of Lavrov, or come to think of it even Paul Graham. 
In fact, I was unable to write a single interesting macro in Nim. My students 
did not have any success in this endeavor either. Thus, I decided to write 
simple macros, and ask the Nim community for help in improving them.

## Named Let I will start with loop macros. The most desirable goal would be to 
code something like the named-let that is coded on four lines of page 45 in the 
book _Let Over Lambda_ , and works straight out of the box in all 
implementations of Scheme. However, I will start with a repeat macro. This is 
what I came up with:
    
    
    import macros, strutils, os
    
    macro theMagicWord(statments: untyped): untyped =
        result = statments
        for st in statments:
              for node in st:
                    if node.kind == nnkStrLit:
                         node.strVal = node.strVal & ", Please."
    
    macro rpt(ix: untyped, cnt: int, statements: untyped)=
      quote do:
        for `ix` in 1..`cnt`:
          `statements`
    
    rpt j, paramStr(1).parseInt :
      theMagicWord:
         echo j, "- Give me some bear"
         echo "Now"
    
    
    Run

As you noted from the above code, I read Steve Kellock's article _Nim Language 
Highlights_ and I liked it. In the book that I am writing, I intend to explain 
how the rpt macro works, and also how to reason about macro coding in Nim. 
However, I must confess that I still don't have any clear ideas of how to 
design a Nim macro. I am not even sure whether a multi-tool such as the Lisp 
loop is viable in Nim. To give an opinionated answer, I think that Lisp SEXPR 
is very flexible, a ball of mud, in the words of Joel Moses. I am afraid that I 
would not be able to deal with Nim trees as easily as I can shape, mold and 
cast Lisp SEXPRs.

Right now, I am accepting suggestions both on how to code a named-let and also 
on how to improve my rpt macro. For instance, I would like to write something 
like the following:
    
    
    rpt paramStr(1).parseInt times -> j:
      theMagicWord:
         echo j, "- Give me some bear"
         echo "Now"
    
    
    Run

I am gladly awaiting your suggestions, and macro examples. It is, from a 
didactic point of view, desirable that the examples be limited to 5 lines, as 
is the case of chapter 3 in _Let over Lambda_.

Reply via email to