7 lines of code, 3 minutes: Implement a programming language from scratch
source : 7 lines of code, 3 minutes: Implement a programming language
[[Lambda calculus]] has three fundamental operations: variable references, anonymous functions and function calls.
\(\lambda v . e\) is comperable to
If we wanted to define a lambda calculus interpreter, it would be trivial to express that:
eval : Expression * Environment -> Value apply : Value * Value -> Value Environment = Variable -> Value Value = Closure Closure = Lambda * Environment
It also has a trivial implementation in [[Racket]]:
#lang racket ; bring in the match library: (require racket/match) ; eval matches on the type of expression: (define (eval exp env) (match exp [`(,f ,e) (apply (eval f env) (eval e env))] [`(λ ,v . ,e) `(closure ,exp ,env)] [(? symbol?) (cadr (assq exp env))])) ; apply destructures the function with a match too: (define (apply f x) (match f [`(closure (λ ,v . ,body) ,env) (eval body (cons `(,v ,x) env))])) ; read in, parse and evaluate: (display (eval (read) '())) (newline)
The rest of this post has a longer interpreter and some worthwhile links.
📖 Open document (Hedgedoc) at https://doc.anagora.org/7-lines-of-code-3-minutes-implement-a-programming-language-from-scratch 📖 Open document (Etherpad) at https://stoa.anagora.org/p/7-lines-of-code-3-minutes-implement-a-programming-language-from-scratch 📹 Video conferencing space (Jitsi Meet) at https://meet.jit.si/7-lines-of-code-3-minutes-implement-a-programming-language-from-scratch
🔎 Full text search for [[7 lines of code 3 minutes implement a programming language from scratch]]