Skip to content

Instantly share code, notes, and snippets.

@fogus
Last active October 30, 2025 16:28
Show Gist options
  • Save fogus/84d55d04979f2cbe82ad305c41661795 to your computer and use it in GitHub Desktop.
Save fogus/84d55d04979f2cbe82ad305c41661795 to your computer and use it in GitHub Desktop.
(defn eval-expr [expr env]
(cond (symbol? expr) (env expr)
(and (seq? expr) (= 'lambda (first expr)))
(let [[_ [a] body] expr]
(fn [arg]
(eval-expr body (fn [x] (if (= a x)
arg
(env x))))))
(seq? expr)
(let [rtor (eval-expr (first expr) env)
rnd (eval-expr (second expr) env)]
(rtor rnd))
:else
(throw (ex-info "uncool" {:expr expr}))))
;; test code
(eval-expr '((lambda (n) n) hello) {'hello "hi!"})
(eval-expr '(((lambda (x)
(lambda (y)
x))
foo)
bar)
'{foo 1, bar 2})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment