Skip to content

aaryanshroff/tiny-lisp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Lisp

A Lisp interpreter and REPL written in Typescript with support for mutation, lambda functions, conditionals and lists.

Running the REPL

  1. Install required dependencies (typescript and ts-node):
    npm install
    
  2. Run main.ts file with ts-node:
    npx ts-node main.ts
    

Sample Program(s)

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
50

Tail 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

Language Features

Variables

(define variable-name value)

> (define x 5)
> x
5

(set! existing-variable new-value)

> (set! x 10)
> x
10

Lambda functions

(define function-name (lambda (params) (body)))

> (define sum (lambda (a b) (+ a b)))
> (sum 5 3)
8

Conditionals

(if (condition) (then) (else))

> (if (> 5 3) (quote Greater) (quote Less))
Greater
> (if (> 5 10) (quote Greater) (quote Less))
Less

Lists

Empty 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

> (begin (define L 5) (+ L 10))
15

References

Inspired by Peter Norvig's Lisp in 90 lines of Python

About

Simple Lisp interpreter and REPL in TypeScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors