Implementing let in Clojure itself

In Clojure, the let macro can be implemented in an elegant way using just what the language provides out of the box.

;; An imcomplete `let` implementation.
(defmacro my-let [bindings & body]
  `((fn [~@(take-nth 2 bindings)] ~@body) ~@(take-nth 2 (rest bindings))))

In action:

(comment
  ;; Standard let
  (let [name "Bojack"]
    (println name))
  ;; Using the custom implementation of the let form.
  (my-let [name "Bojack"]
    (println name))
  :rfc)

Why does it work?

Let's examine the macro expansion:

((fn* ([name] (println name))) "Bojack")

The macro expands into an immediately invoked anonymous function. The function's parameters become the local bindings, and the arguments passed to it become the bound values. This leverages Clojure's lexical scoping to create the exact same binding semantics that let provides.

What's nice here is how Clojure's core abstractions are so well designed that you can rebuild its own special forms using nothing but functions and macros.