A Lisp interpreter and REPL written in Typescript with support for mutation, lambda functions, conditionals and lists.
- Install required dependencies (
typescriptandts-node):npm install - Run
main.tsfile withts-node:npx ts-node main.ts
Simple bank
> (define balance 0)
> (define deposit (lambda (amt) (set! balance (+ balance amt))))
> (define withdraw (lambda (amt) (if (>= balance amt) (set! balance (- balance amt)) (quote Unsuccessful))))
> balance
0
> (deposit 100)
> balance
100
> (withdraw 200)
Unsuccessful
> (withdraw 50)
> balance
50Tail recursive nth fibonacci
> (define fib-tr (lambda (n next result) (if (= n 0) result (fib-tr (- n 1) (+ next result) next))))
> (define fib (lambda (n) (fib-tr n 1 0)))
> (fib 47)
2971215073
evaluate: 1.431ms(define variable-name value)
> (define x 5)
> x
5(set! existing-variable new-value)
> (set! x 10)
> x
10(define function-name (lambda (params) (body)))
> (define sum (lambda (a b) (+ a b)))
> (sum 5 3)
8(if (condition) (then) (else))
> (if (> 5 3) (quote Greater) (quote Less))
Greater
> (if (> 5 10) (quote Greater) (quote Less))
LessEmpty list
> (define L (list ))
> L
[]list notation
> (define L (list 5 3 1))
> (car L)
5
> (cdr L)
[ 3, 1 ]cons notation
> (define L (cons 5 (cons 3 (cons 1 (list )))))> (begin (define L 5) (+ L 10))
15Inspired by Peter Norvig's Lisp in 90 lines of Python