LISP Interpreter written in LDPL.
(quote x)
returnsx
without executing it.(begin x1 ... xn)
executesx1
,x2
, ...,xn
and returns the result ofxn
.(define x y)
defines the variablex
with the value ofy
enlarging the scope.(set x y)
sets the value ofx
to the value ofy
without enlarging the scope, the variablex
already has to be defined.(if c1 x1 c2 x2 ... cn xn [y])
if the conditionc1
is true, returns the result ofx1
. If it is not, it keeps evaluating until any of the conditionsc1
,c2
, ...,cn
are true. If none of these return true, it returnsy
. The[y]
part is optional.(lambda (x1 ... xn) y1 ... ym)
creates a lambda with n parameters that when called executes the commandsy1
, ...,yn
.(f x1 ... xn)
calls the functionf
with the parametersx1
, ...,xn
.()
is the empty list.(cons x y)
creates a cons(x . y)
(car x)
returns the car of the cons.(cdr x)
returns the cdr of the cons.(null x)
if the list is empty returnst
, else returns()
.(eq x y)
ifx
is equal toy
returnst
, else returns()
.(print x)
displaysx
.(println x)
displaysx
followed by a line break.
(define nil (quote ()))
(define map)
(set map
(lambda (f list)
(if (null list)
nil
(cons
(f (car list))
(map f (cdr list))))))
(println
(map (lambda (x) (cons x x))
(cons (quote a)
(cons (quote b)
(cons (quote c)
nil)))))
- How to run the example:
$ ./ambulisp < map.ambulisp
- The example outputs
((a . a) (b . b) (c . c))
.
More examples can be found in the repository.