0

Hey everyone so I'm looking for a way to convert a lisp macro to work in Clojure I looked at some other posts talking about if you could convert but they really didn't show an example and was maybe looking for some help on this.

(defmacro n-of (n form) 
  (let ((lst-sym (gensym)) 
        (i-sym (gensym))) 
    `(let ((,lst-sym ())) 
       (dotimes (,i-sym ,n) 
         (push ,form ,lst-sym)) 
       (nreverse ,lst-sym))))

1 Answer 1

2

For lisp macro i assume Common Lisp macro.

(defmacro n-of [n & form]
  `(let [f# #(do ~@form)]
     (take ~n (repeatedly f#))))

where the & is like &rest or &body. #() is (lambda...). ~@ is ,@-. '~' is ','.

Sign up to request clarification or add additional context in comments.

2 Comments

...and the f# nicely saves us having to invoke gensym.
If we go with the original in which the form is a single sexpr, could we do?: (defmacro n-of [n form] `(take ~n (repeatedly ~form)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.