jcheat
jcheat

Reputation: 121

Common Lisp macro for "let" to match Clojure

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))

Upvotes: 3

Views: 202

Answers (1)

sds
sds

Reputation: 60064

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.

Upvotes: 10

Related Questions