scheme-on-coffee
scheme-on-coffee copied to clipboard
Toy Scheme interpreter written in CoffeeScript
Lisp/Scheme evaluator, see: SICP, 4.1
Port to CoffeeScript
by Dmitry Soshnikov [email protected]
(C) MIT Style License
Examples (see http://dmitrysoshnikov.com/coffee-lisp-eval/):
-
Sum:
Input:
(+ 1 2 3)Output:
6 -
Define a variable in the Global environment:
Input:
(define x 10)Output:
10 ; now x can be referred as bound variable -
Define a procedure:
Input:
(define (sum x y) (+ x y))Output:
ok ; procedure object created -
Eval the sequence (block) of expression:
Input:
(begin (define a 10) ; define "a" variable (define b 30) ; and "b" variable (define (square x) (* x x)) ; and a function (+ (square a) b)) ; and get the sum of square of "a" and "b"Output:
130 -
Recursion; define the factorial function an call it:
Input:
(begin (define (factorial n) ; define the factorial function (if (= n 1) 1 (* n (factorial (- n 1))))) (factorial 5)) ; and call it for value 5Output:
120 -
Map the list with a lambda expression:
Input:
(map (lambda (x) (* x x)) ; anonymous function (list 1 2 3 4)) ; maps the list to '(1 4 9 16)Output:
(1 4 9 16)