3

Clojure's let is more concise than Common Lisp with less parentheses:

 ;Clojure
(let [a 1 b 2]
     (+ a b))


;Common Lisp
(let ( (a 1) (b 2))
    (+ a b))

How would you write a macro in Common Lisp to be equivalent?:

(letmac ( a 1 b 2)
    (+ a b))
jcheat
  • 121
  • 6

1 Answers1

10

This is not too hard:

(defmacro clojure-let (bindings &body body)
  `(let ,(loop for (a b) on bindings by #'cddr collect (list a b))
     ,@body))

see how it works:

> (macroexpand-1 '(clojure-let (a b c d) (foo) (bar)))
(LET ((A B) (C D)) (FOO) (BAR)) ;
T

However, this is not a very good idea (and not a new one either!):

Your code might be more readable for a clojure user, but it will be less readable for a lisper.

A clojurer might get a false sense of security while a lisper will get confused.

Do not kid yourself

Porting Common Lisp code to Clojure is far harder than writing a few simple macros.

Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278
  • 3
    If you're going to mimic Clojure's `let` (a dubious idea, I agree), you might as well emit `let*` instead of just `let`, since Clojure's `let` does its bindings sequentially rather than all at once. – amalloy Jul 18 '14 at 18:57