1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

zenlisp reference
By Nils M Holm, 2007, 2008
Feel free to copy, share, and modify this document.
See the file LICENSE for details.
0 Contents
1 . . . . . . . . . . . . . . Forms
1.1 . . . . . . . . . . Abbreviations
1.2 . . . . . . . . . . . . . Comments
1.3 . . . . . . . . . Unreadable Forms
2 . . . . . . . . . . . Expressions
2.1 . . . . . . . . . . . . . Symbols
2.2 . . . . . . . . . . . . Functions
3 . . . . . . . . . . . Some Theory
3.1 . . . . . . . . . Lambda Functions
3.2 . . . . . . . Function Application
4 . . . . . . . Primitive Functions
4.1 . . Composition and Decomposition
4.2 . . . . . . . Binding Constructcs
4.3 . . . . . . . . . . . . Predicates
4.4 . . . . . . . . . . . Control Flow
4.5 . . . . . . . . . . REPL Functions
4.6 . . . . . . . . . . Meta Functions
5 . . . . . . . . Utility Functions
5.1 . . . . . . . . . . List Functions
5.2 . . . . . . . . . . . . Predicates
5.3 . . . . . . . . . . . Control Flow
5.4 . . . . . . . . . . . . . Packages
6 . . . . . . . . . . Math Functions
6.1 . . . . . . . . . . . . . Summary
7 . . . . . . . . . . . Miscellanea
7.1 . . . . . . . . Naming Convention
7.2 . . . . . . . . Evaluation History
7.3 . . . . . . . . . . . Source Path
1 Forms
A Symbol is any combination of these characters:
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 0
* +  / < = >
An Atom is either a symbol or () (pronounced NIL).
A Pair is a concatenation of two forms:
(carpart . cdrpart)
A pair may contain other pairs:
((a . b) . c)
(a . (b . c))
((a . b) (c . d))
Each Form is either an atom or a pair.
1.1 Abbreviations
Some pairs may be abbreviated:
(a . ()) = (a)
(a . (b)) = (a b)
(a . (b . c)) = (a b . c)
A List is a pair whose innermost cdr part is ():
List = () or (form . list).
These are Lists:
()
(foo)
(foo bar baz)
((a . b) foo (nested list))
A list whose innermost cdr part is a symbol is called
a Dotted List. These are dotted lists:
(a b . c)
((foo bar) . baz)
Lists of singlecharacter symbols can be condensed:
(a) = '#a
(a b c) = '#abc
( 2 5 7) = '#257
1.2 Comments
A comment may be inserted anywhere (even inside of a form)
by including a semicolon (;). Comments extend to the end of
the current line.
Example:
(define (f x) ; this is a comment
(cons x x))
1.3 Unreadable Forms
A form that is delimited by curly braces is unreadable:
{no matter what} => undefined
Unreadable forms are used to represent data that have no
unambiguous textual representation.
2 Expressions
An Expression is a form with a meaning.
x => y denotes that x reduces to y;
y is the normal form of x.
bottom denotes an undefined value.
2.1 Symbols
Each symbol reduces to the value bound to it:
Symbol => value of symbol
UndefinedSymbol => bottom
A symbol that is bound to itself is called a Constant,
symbols bound to other values are called Variables.
2.2 Functions
(F x) denotes the application of f to x. F is called a
Function. X is called an Argument.
(function) => normal form
(function form) => normal form
(function form ...) => normal form
Function applications are reduced by first reducing arguments
to their normal forms and then applying the Function to the
resulting normal forms.
Pseudo Functions are constructs that are applied in the same
way as functions but do not reduce their arguments.
3 Some Theory
3.1 Lambda Functions
(Lambda (x) e) is a Lambda Function.
X is a variable of that function and E is the Term of that
function. Lambda functions are anonymous.
If X does not occur in E, the function is constant.
X may occur multiple times in E.
X is bound in an expression E, if
 E is a lambda function
AND
 X is a variable of E.
Examples:
X is bound in (lambda (x) x).
Y is bound in (lambda (y) (lambda (x) (x y)))
Y is not bound in (lambda (x) (x y))
When a variable X is not bound in an expression E,
X is free in E; X is a free variable of E.
3.2 Function Application
A lambda function is applied to an expression using
Beta Reduction.
e[x/v] means: replace each X that is free in E with V.
Beta reduction:
((lambda (x) e) v) => e[x/v]
Examples:
a > b denotes a partial reduction; b is not the normal
form of a, because it can be reduced further.
((lambda (x) x) :t) => :t ; identity
((lambda (x) (x x)) :t) > (:t :t) ; selfapplication
((lambda (x) (f x)) :t) > (f :t) ; f is free
((lambda (x) ()) :t) => () ; constant function
Functions of multiple variables bind arguments by position:
((lambda (x y z) (list x y z)) 'first 'second 'third)
=> '(first second third)
4 Primitive Functions
The following definitions apply:
symbol denotes a variable.
'symbol denotes a constant.
eval[x] denotes the normal form of x.
x ... denotes zero or more appearances of x.
x  y denotes either x or y.
Variables become constants when passed as arguments to pseudo
functions.
A list of pairs is called an Association List (or Alist).
The car part of each pair of an alist is called its key and
its cdr part is called its value.
4.1 Composition and Decomposition
4.1.1 CAR
(car pair) => form
Extract the car part of a pair.
Examples:
(car '(a.b) => 'a
(car '(a)) => 'a
(car '#abc)) => 'a
(car '((ab) c)) => '(ab)
(car 'a) => bottom
(car ()) => bottom
4.1.2 CDR
(cdr pair) => form
Extract the cdr part of a pair.
Examples:
(cdr '(a.b) => 'b
(cdr '(a)) => ()
(cdr '#abc)) => '#bc
(cdr '((ab) c)) => '(c)
(cdr 'a) => bottom
(cdr ()) => bottom
4.1.3 CONS
(cons form form) => pair
Construct a fresh pair.
Examples:
(cons 'foo 'bar) => '(foo . bar)
(cons 'foo ()) => '(foo)
(cons 'foo '(bar)) => '(foo bar)
(cons 'a '(b . c) => '(a b . c)
(cons () () => (())
(cons '(foo) '(bar)) => '((foo) bar)
4.1.4 EXPLODE
(explode symbol) => list
Explode a symbol to a list of singlecharacter symbols. If
the argument is (), return ().
Examples:
(explode 'foo) => '(f o o) = '#foo
(explode 'x) => '(x) = '#x
(explode ()) => ()
(explode '(a.b)) => bottom
4.1.5 IMPLODE
(implode list) => symbol
Implode a list of singlecharacter symbols to a symbol. If
the argument is (), return ().
Examples:
(implode '(f o o)) => 'foo
(implode '#foo) => 'foo
(implode '(x)) => 'x
(implode ()) => ()
(implode '(a (b.c))) => bottom ; nonatom in list
(implode '(a bc)) => bottom ; symbol BC too long
4.1.6 QUOTE
(quote form) => 'form
Indicate normal form.
Examples:
foo => eval[foo]
(quote foo) => 'foo
(cons :t :t) => '(:t . :t)
(quote (cons :t :t)) => '(cons :t :t)
Note: 'foo is just an abbreviation of (quote foo).
4.2 Binding Constructcs
4.2.1 DEFINE (pseudo function)
(define symbol form) => 'symbol
Bind eval[form] to symbol.
Examples:
(define foo 'bar) => 'foo ; bind foo to 'bar
(define f (lambda (x) x)) => 'f ; bind f to (lambda (x) x)
(define (f x) x) => 'f ; bind f to (lambda (x) x)
(define (f x . y) y) => 'f ; bind f to (lambda (x . y) y)
(define (f . x) x) => 'f ; bind f to (lambda x x)
4.2.2 LAMBDA (pseudo function)
(lambda (symbol ...) form) => (closure (symbol ...) form env)
Create a closure from a lambda expression.
A Closure is a snapshot of a lambda function at a given time.
In fact, only closures are valid function in zenlisp while
lambda expressions merely create closures.
The snapshot is taken by capturing the names and values of
all free variables of the term of the lambda function and
storing them in an alist. This alist is attached to the
closure as the ENV argument.
When a closure is applied, the captured bindings be will
reestablished during the application.
Lambda may have zero arguments or more than a single argument:
((lambda () :t)) => :t
((lambda (x y z) z) 'a 'b 'c) => 'c
Variadic arguments are implemented using dotted argument
lists:
((lambda (x . y) y) 'a) => ()
((lambda (x . y) y) 'a 'b) => '(b)
((lambda (x . y) y) 'a 'b 'c) => '(b c)
When the argument list is atomic, all arguments are bound
to that atom:
((lambda x x)) => ()
((lambda x x) 'a) => '(a)
((lambda x x) 'a b c) => '(a b c)
Examples:
(lambda (x) x) => (closure (x) x ())
(lambda (x) (lambda (y) (cons x y)))
=> (closure (x) (lambda (y) (cons x y)) ())
((lambda (x) (lambda (y) (x y))) 'foo)
=> (closure (y) (x y) ((x . foo)))
4.2.3 LET (pseudo function)
(let ((symbol form) ...) term) => eval[term]
LET is an alternative syntax for the application of a
lambda function:
(let ((f1 a1) ... (fN aN)) expr)
equals
((lambda (f1 ... fN) expr) a1 ... aN)
The first argument of LET is called its environment. It is
a list of twoelement lists called bindings. The first
element of each binding holds the name of the symbol to
bind and the second element holds the value to be bound.
The term of LET is reduced in the local context created by
establishing the bindings of the environment. The context
ceases to exist after reducing the term to its normal form.
The purpose of LET is to name intermediate results in
expressions:
(let ((f (lambda (x) (cons x x)))
(v 'foo))
(f v))
=> '(foo . foo)
LET first reduces all values of the environment before
it binds any symbols. Therefore,
(let ((v :f))
(let ((v :t)
(u v)) ; U is bound to the outer value of V
u))
=> :f
4.2.4 LETREC (pseudo function)
(letrec ((symbol form) ...) term) => eval[term]
LETREC works like LET, but in addition it fixes recursive
bindings using RECURSIVEBIND (see below).
Therefore LETREC can be used to bind recursive functions
(even mutually recursive ones) to symbols.
4.2.5 RECURSIVEBIND
(recursivebind '((symbol . form) ...))
RECURSIVEBIND fixes recursive references in environments.
Its argument is an environment represented by an alist.
A recursive reference occurs when a closure closes over
the symbol it is bound to:
((f . (closure (x) (f x) ((f . void)))))
Because F is closed over before it is bound to the closure,
F cannot recurse. Passing above environment to RECURSIVEBIND
yields the following recursive structure:
((f . (closure (x) (f x)
((f . (closure (x) (f x)
((f . (closure (x) (f x)
((f . ...
4.3 Predicates
4.3.1 ATOM
(atom form) => :t  :f
Reduce to :t, if the given form is atomic and otherwise to :f.
Examples:
(atom ()) => :t
(atom :t) => :t
(atom 'foo) => :t
(atom '(a.b)) => :f
(atom '(a b)) => :f
(atom '#foo) => :f
4.3.2 DEFINED
(defined 'symbol) => :t  :f
Reduce to :t, if the given symbol is bound in any active
context (ie by DEFINE or in a surrounding LET or LETREC).
Otherwise reduce to :f.
Examples:
(defined 'undefined) => :f
(defined 'defined) => t
(defined '(a.b)) => bottom
(defined '#foo) => bottom
4.3.3 EQ
(eq form1 form2) => :t  :f
Reduce to :t, if the given forms are identical and otherwise
to :f.
Two forms are identical, if they are the same symbol, bound
to the same symbol, or if they are both ().
Examples:
(eq 'foo 'foo) => t
(eq foo foo) => t
(eq :f :f) => t
(eq 'foo 'bar) => :f
(eq 'foo '#foo) => :f
(eq 'a '(a.b)) => :f
(eq '#foo '#foo) => bottom
(eq '(a.b) '(a.b)) => bottom
4.4 Control Flow
4.4.1 AND (pseudo function)
(and expr ...) => form
Reduce the given expressions in sequence until one of them
reduces to :f. If one of the expressions reduces to :f,
return :f, otherwise return the normal form of the last
expression. If no expression is given, return :t.
Examples:
(and) => t
(and 'foo) => 'foo
(and :f) => :f
(and :f 'foo) => :f
(and 'foo :f) => :f
(and 'foo 'bar) => 'bar
(and 'a 'b 'c :f) => :f
4.4.2 APPLY
(apply fun expr ... list) => form
Apply the function fun to the given argument list, returning
the normal form of the application. When one or multiple
expressions are given between the function and the list, cons
their normal forms to the list before applying the function.
Note: APPLY is called by value, but fun is applied to list
using call by name.
Examples:
(apply cons '(a b)) => '(a . b)
(apply cons '('a 'b)) => '('a . 'b)
(apply list 'a 'b '(c)) => '(a b c)
(apply (lambda () 'foo) ()) => 'foo
4.4.3 BOTTOM
(bottom form ...) => bottom
Reduce to bottom, thereby stopping the reduction in progress.
The given forms print in the resulting error message.
Examples:
(bottom) => bottom
(bottom 'foo) => bottom
(bottom 'foo 'bar 'baz) => bottom
4.4.4 COND (pseudo function)
(cond (pred expr) (pred expr) ...) => form
Reduce expressions conditionally.
Each argument of COND is a called a clause. It consists of
two expressions:
(predicate expression)
COND reduces the predicate of the first clause and if it
has a true normal form (anything but :f), the entire
application of COND reduces to the normal form of the
associated expression.
COND keeps evaluating clauses until it finds one with a
true predicate. At least one predicate must be true.
Examples:
(cond ('foo 'bar)) => 'bar
(cond (:f 'foo) (t 'bar)) => 'bar
(cond ((atom ()) (cons 'foo 'bar))) => '(foo . bar)
(cond (:f 'oops)) => bottom
4.4.5 OR (pseudo function)
(or expr ...) => form
Reduce the given expressions in sequence until one of them
reduces to :t. If one of the expressions reduces to :t,
return :t, otherwise return the normal form of the last
expression. If no expression is given, return :f.
Examples:
(or) => :f
(or 'foo) => 'foo
(or :f) => :f
(or :f 'foo) => 'foo
(or 'foo :f) => 'foo
(or 'foo 'bar) => 'foo
(or :f :f :f 'a) => 'a
4.5 EVAL
(eval expr) => form
Reduce expr and return its normal form.
Examples:
(eval '(cons 'a 'b)) => '(a . b)
(eval (cons 'a 'b)) => bottom ; = (eval '(a . b))
4.6 Meta Functions
These functions are designed to be applied at the REPL.
They are not intended for use in programs.
4.6.1 CLOSUREFORM (pseudo function)
(closureform args  body  env) => argument  :f
Preset the amount of information to be disclosed when
printing a closure.
(closureform args) ; this is the default
(lambda (foo) bar) => {closure (foo)}
(closureform body) ; also print the body
(lambda (foo) bar) => {closure (foo) bar}
(closureform env) ; also print the environment
; given that BAR is bound to BAZ
(lambda (foo) bar) => (closure (foo) bar ((bar . baz)))
NOTE: (closureform env) may cause the interpreter to emit
*a lot* of information. Printing recursive closures (created
using LETREC or RECURSIVEBIND) may take forever. Literally.
Incomplete closures are unreadable because their textual
representation is ambiguous.
4.6.2 DUMPIMAGE (pseudo function)
(dumpimage filename) => t
Dump an image of the interpreter workspace to the given file.
Reduce to :t on success and bottom in case of failure.
An image dump may be reloaded by passing its file name to
the interpreter.
4.6.3 GC (pseudo function)
(gc) => (freenodes maxuse)
Perform a garbage collection and return some information.
Freenodes is the amount of free nodes in the workspace.
Maxuse is the maximum number of live nodes since the the
last application of GC.
4.6.4 LOAD (pseudo function)
(load filename) => t
Read the content of the given file as if typed in at the
interpreter prompt. A .l suffix will be attached to the
given file name, so
(load foo)
will in fact load the file "foo.l".
4.6.5 QUIT
(quit) =>
Terminate the interpreter.
4.6.6 STATS (pseudo function)
(stats expr) => '(normalform steps nodes gcs)
Reduce the given expression to its normal form. Return that
normal form plus some additional information.
STEPS is the number of reduction steps performed before
the normal form was found.
NODES is the total number of nodes allocated during the
reduction.
GCS is the number of garbage collections performed during
the reduction.
The information delivered by STATS may be used to compare
algorithms.
4.6.7 SYMBOLS
(symbols) => list
Return a list of all symbols in the symbol table.
4.6.8 TRACE (pseudo function)
(trace functionname) => :t
Tell the interpreter to print applications of the given
function before applying it.
Use (trace) to turn off tracing.
4.6.9 VERIFYARROWS
(verifyarrows :t  :f) => :t  :f
Turn verification of reduction operators on or off.
When verification is off, arrow operators (=>) at the top
level act as comments:
(verifyarrows :f)
(cons 'a 'b) => this is a comment
=> '(a . b)
When verification is on, zenlisp will make sure that the
normal form of the expression on the lefthand side of =>
is equal to the form on its righthand side:
(verifyarrows :t)
(cons 'a 'b) => '(a . b)
=> '(a . b)
When the verification succeeds, nothing special happens.
When the verification fails, an error message is issued:
(cons 'a 'b) => 'foo
=> '(a . b)
* 1: REPL: Verification failed; expected: foo
5 Utility Functions
5.1 List Functions
5.1.1 APPEND
(append list ...) => list
Concatenate lists. Appending () to a list yields the
original list. Appending an atom to a list yields a
dotted list.
Examples:
(append '(foo bar) '(baz)) => '(foo bar baz)
(append '#abc '#def '#xyz) => '#abcdefxyz
(append () '#foo) => '#foo
(append '#foo ()) => '#foo
(append '(a) '(b . c)) => '(a b . c)
(append '#abc 'd) => '(a b c . d)
(append () ()) => ()
(append ()) => ()
(append) => ()
5.1.2 ASSOC / ASSQ
(assoc form alist) => pair
Retrieve a pair with a given key from an association list.
Return :f if no pair has a matching key.
Examples:
(assoc 'b '((a.1) (b.2))) => '(b . 2)
(assoc 'x '((x.1) (x.2))) => '(x . 1)
(assoc 'q '((x.1) (x.2))) => :f
(assoc '#foo '((#foo . bar))) => '(#foo . bar)
ASSQ is similar to ASSOC, but its first argument is limited
to symbols:
(assq 'b '((a.1) (b.2))) => '(b . 2)
(assq '#foo '((#foo . bar))) => :f
5.1.3 CAAR ... CDDDDR
(caar list) = (car (car list))
(cadr list) = (car (cdr list))
(cdar list) = (cdr (car list))
(cddr list) = (cdr (cdr list))
(cddddr list) = (cdr (cdr (cdr (cdr list))))
Extract elements of nested lists:
Examples:
(caar '((key . value)) => 'key
(cdar '((key . value)) => 'value
(cadr '(first second)) => 'second
(caddr '#1234) => '3
(cadddr '#1234) => '4
5.1.4 ID
(id form) => form
Map a value to itself (identity function).
(OK, not really a list function.)
Examples:
(id 'foo) => 'foo
(id (id '#foo)) => '#foo
5.1.5 LIST
(list expr ...) => list
Form a list from arguments. Unlike members of quoted (constant)
lists, the arguments of LIST are reduced before inserting them
in the list.
Examples:
(list) => ()
(list 'foo) => '(foo)
(list 'a 'b 'c) => '#abc
'((cons 'a 'b)) => '((cons 'a 'b))
(list (cons 'a 'b)) => '((a . b))
5.1.6 MEMBER / MEMQ
(member expr list) => list
(memq symbol list) => list
Find a member of a list.
Examples:
(member 'bar '(foo bar baz)) => '(bar baz)
(member '(b.2) '((a.1) (b.2))) => '((b . 2))
(member 'foo '(a b c d e f)) => :f
MEMQ is like MEMBER, but its first atgument is limited to
symbols:
(memq 'bar '(foo bar baz)) => '(bar baz)
(memq '(b.2) '((a.1) (b.2))) => :f
5.1.7 REVERSE
(reverse list) => list
Create a reverse copy of a list:
Examples:
(reverse '(foo bar)) => '(bar foo)
(reverse '(a b c d e f)) => '#fedcba
(reverse ()) => ()
(reverse '(a . b)) => bottom
(reverse '(a b c . d)) => bottom
5.2 Predicates
5.2.1 EQUAL
(equal form form) => :t  :f
Return :t if the two given forms are equal and otherwise :f.
Two forms are equal, if they are both the same symbol or if
they are pairs containing equal car and cdr parts.
Examples:
(equal () ()) => t
(equal '(a.b) '(a.b)) => t
(equal '(f (f x y) z) '(f (f x y) z)) => t
(equal '#abcdef '#abcdef) => t
(equal 'foo 'bar) => :f
(equal '(x (y) z) '(x (q) z)) => :f
(equal '#xxx '#xxy) => :f
5.2.2 LISTP
(listp form) => :t  :f
Return :t if the given form is a (nondotted) list and
otherwise :f.
Examples:
(listp ()) => t
(listp '(a b c)) => t
(listp '#abcdef) => t
(listp '(a . b)) => :f
(listp '(a b . c)) => :f
(listp 'foo) => :f
5.2.3 NEQ
(neq form form) => :t  :f
Return :t if the given forms are not identical and otherwise :f.
Examples:
(neq 'foo 'bar) => t
(neq 'foo '#foo) => t
(neq 'a '(a.b)) => t
(neq 'foo 'foo) => :f
(neq foo foo) => :f
(neq () ()) => :f
(neq '#foo '#foo) => bottom
(neq '(a.b) '(a.b)) => bottom
5.2.4 NOT
(not form) => :t  :f
Check whether the given form is :f (logical negation).
Examples:
(not :f) => :t
(not ()) => :f
(not t) => :f
(not 'foo) => :f
(not '(a b c)) => :f
5.2.5 NULL
(not form) => :t  :f
(null form) => :t  ()
Check whether the given form is ().
Examples:
(null ()) => t
(null :f) => :f
(null 'x) => :f
(null '(a b c)) => :f
5.3 Control Flow
5.3.1 FOLD
(fold fun form list) => form
Fold the given list by combining FORM with its first element
using the binary function FUN. Combine the result with the
second member, etc:
(fold f () (a b c d)) = (f (f (f (f () a) b) c) d)
If LIST is empty, return FORM.
Examples:
(fold cons 'a '(b)) => '(a . b)
(fold cons 'a '(b c d)) => '(((a . b) . c) . d)
(fold cons 'a ()) => 'a
5.3.2 FOLDR
(foldr fun form list) => form
Fold the given list by combining its head with its reduced
tail using the binary function FUN.
While FOLD combines its arguments leftassociatively,
FOLDR combines them rightassociatively:
(foldr f () (a b c d)) = (f a (f b (f c (f d ()))))
If LIST is empty, return FORM.
Examples:
(foldr cons a '(b)) => '(a . b)
(foldr cons 'a '(b c d)) => '(a b c . d)
(foldr cons 'a ()) => 'a
5.3.3 MAP
(map fun list list ...) => list
Map the given function over the given list(s). The function
must take the same number of arguments as there are lists.
The N'th member of the resulting list is the result of
applying FUN to the N'th members of all input list.
Examples:
(map car '((a) (b) (c))) => '#abc
(map cdr '((a) (b) (c))) => '(() () ())
(map cons '(a b c) '(d e f)) => '((a . d) (b . e) (c . f))
(map list '(a b) '(c d) '(e f)) => '(#ace #bdf))
5.4 Packages
5.4.1 REQUIRE
(require 'packagename) => :t  :f
Load a package (using LOAD) if it is not already present.
REQUIRE checks the presence of a package by testing whether
the given package name is defined. Packages are required to
define that name *before* requiring other packages.
Examples:
(require 'nmath) => :t ; load natural math functions
(require 'nmath) => :f ; already loaded
6 Math Functions
zenlisp implements math numbers as lists of digits:
123 is written as '(1 2 3) or '#123.
Rational numbers are numbers containing a slash:
5/4 is written as '( 5 / 4) or '#5/4.
Math functions are not part of the default image. To load
them use
(load nmath) ; load natural math functions
or
(load imath) ; load integer math functions (includes nmath)
or
(load rmath) ; load rational math functions (includes imath)
To create an image with all math functions, type
(load rmath)
(dumpimage mathimage)
and run zenlisp using
zl mathimage
6.1 Summary
... indicates repetition.
[x] indicates that x is optional.
xy indicates x or y.
x = number;
n = natural;
i = integer;
r = rational.
Function Returns...
(* [x ...]) => x product
*epsilon* => n log10 of precision of SQRT
(+ [x ...]) => x sum
( x1 x2 [...]) => x difference
( x) => x negative number
(/ x1 x2) => x ratio
(< x1 x2 [...]) => :t:f :t for strict ascending order
(<= x1 x2 [...]) => :t:f :t for strict nondescending order
(= x1 x2 [...]) => :t:f :t for equivalence
(> x1 x2 [...]) => :t:f :t for strict descending order
(>= x1 x2 [...]) => :t:f :t for strict nonascending order
(abs x) => x absolute value
(denominator r) => i denominator
(divide i1 i2) => (i3 i4) quotient i3 and remainder i4
(even i) => :t:f :t, if i is even
(expt x i) => x x to the power of i
(gcd [i1 ...]) => n greatest common divisor
(integer x) => i an integer with the value x
(integerp x) => :t:f :t, if x is integer
(lcm [i1 ...]) => n least common multiple
(length list) => n length of a list
(limit op x1 ...) => x find the limit of x1... under op
(max x1 [x2 ...]) => x maximum value
(min x1 [x2 ...]) => x minimum value
(modulo i1 i2) => i3 modulus
(natural x) => n a natural with the value x
(naturalp x) => :t:f :t, if x is natural
(negate ir) => ir negative value
(negative x) => :t:f :t, if x is negative
(numberp expr) => :t:f :t, if expr represents a number
(numerator r) => i numerator
(odd i) => :t:f :t, if i is not even
(one x) => :t:f :t, if x equals one
(quotient i1 i2) => i quotient
(rational x) => r a rational with the value x
(rationalp x) => :t:f :t, if x is rational
(remainder i1 i2) => i division remainder
(sqrt n) => x square root, see also *espilon*
(zero x) => :t:f :t, if x equals zero
[*] The result of SQRT depends on the library in use.
The natural and integer versions return the greatest
natural number whose square is not larger than the
argument.
The rational version returns a number that differs
from the actual square root of the argument by no
more than (/ '#1 (expt '#10 *epsilon*)), where
*epsilon* is a global variable.
7 Miscellanea
7.1 Naming convention
Symbols starting and ending with an asterisk are reserved
for the code implementing zenlisp. They must be avoided in
userlevel code.
7.2 Evaluation History
The normal form most recently produced by the interpreter
is bound to the symbol **:
(car '(first second))
=> 'first
(cons ** **)
=> '(first . first)
7.3 Source Path
When loading code using LOAD or REQUIRE, the following
abbreviations may be used:
(load ~nmath)
(require '~nmath)
both load the file "nmath.l" from the directory specified in the
environment variable ZENSRC.
When a file "foo" that is being loaded loads another file "bar",
the file "bar" is assumed to reside in the same directory as
"foo". When "foo" is loaded using
(load /baz/foo)
the function application
(load bar)
in the file "foo" actually loads /baz/bar.
