Lisp

Lisp evaluator

Evaluates a subset of Common Lisp in the browser, traces calls to user-defined functions the way TRACE prints them, and checks exercises: predicting what expressions return and writing basic functions against test cases.

Code definitions and expressions, evaluated in order

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.
Ctrl + Enter runs

Factorial. A recursive factorial, called on a few numbers; integers have no size limit.

Results 4 forms

  1. (defun fact (n) (if (= n 0) 1 (* n (fact (- n 1)))))

    Value: FACT

  2. (fact 3)

    Value: 6

  3. (fact 20)

    Value: 2432902008176640000

  4. (mapcar #'fact '(0 1 2 3 4 5))

    Value: (1 1 2 6 24 120)

Evaluated 4 forms: 4 values, 0 errors.

REPL one expression at a time

Enter evaluates in the environment of the last Run; ↑ and ↓ recall earlier lines.

Trace 92 lines

Line 1 of 92
Depth 0: call (FACT 3)

Calls in progress: (FACT 3)

(fact 3)

(fact 20)

(mapcar #'fact '(0 1 2 3 4 5))

With focus in the trace: ← → previous and next line, Home End first and last, Space play or pause.

Exercises Interpret · 1 of 16

Type what each expression returns, as Lisp prints it (or error), and Check. Answers are read as Lisp data, so spacing and case do not matter.

CAR and CDR chains

  1. Show answer
  2. Show answer
  3. Show answer
  4. Show answer
Show note

Reference supported forms and functions

  • Symbols are read without regard to case and print in upper case: (car '(a b)) prints A.
  • NIL is both false and the empty list (); every other value counts as true, and T is the usual true value.
  • 'x is short for (quote x): the expression is returned as data, not evaluated.
  • Integers have no size limit; / on integers gives a ratio such as 7/2; decimals are single floats (3.5, 0.33333334).
  • Results print the way Common Lisp prints them: strings in double quotes, dotted pairs as (A . B), functions as #<FUNCTION NAME>.
  • Built-in functions cannot be redefined (Common Lisp’s package lock): use names like MY-LENGTH.
  • Each top-level form may take 200,000 evaluation steps and 1,000 nested function calls.
  • Not supported: LOOP, DO, FLET and LABELS, macros, multiple values, characters, vectors, hash tables and structures.

Definitions and variables

  • defun
  • lambda
  • defvar
  • defparameter
  • let
  • let*
  • setq
  • setf
  • incf
  • decf
  • push
  • pop

SETF, INCF, DECF, PUSH and POP work on variables only. DEFUN takes &optional and &rest parameters.

(defun square (x) (* x x))
returns SQUARE
(let ((x 2) (y 3)) (* x y))
returns 6
(let* ((x 2) (y (+ x 1))) y)
returns 3

Conditionals and control

  • quote
  • if
  • cond
  • case
  • when
  • unless
  • and
  • or
  • not
  • progn
  • prog1
  • dolist
  • dotimes
  • block
  • return
  • return-from
(if (> 3 2) 'yes 'no)
returns YES
(cond ((= 1 2) 'a) (t 'b))
returns B
(or nil 'x)
returns X
(dolist (x '(1 2 3) 'done))
returns DONE

Lists

  • car
  • cdr
  • cons
  • list
  • list*
  • append
  • reverse
  • length
  • first
  • second
  • third
  • fourth
  • fifth
  • sixth
  • seventh
  • eighth
  • ninth
  • tenth
  • rest
  • last
  • butlast
  • nth
  • nthcdr
  • member
  • assoc
  • remove
  • copy-list
  • sort
  • nconc
  • nreverse

cXr combinations up to four letters (cadr, cddr, caar, cdar, caddr, …). MEMBER, ASSOC and REMOVE compare with EQL unless given :test #'equal. NCONC and NREVERSE give the same results as APPEND and REVERSE (nothing is modified in place).

(cons 'a '(b c))
returns (A B C)
(append '(a) '(b) '(c))
returns (A B C)
(member 'b '(a b c))
returns (B C)
(assoc 'y '((x . 1) (y . 2)))
returns (Y . 2)
(nth 1 '(a b c))
returns B

Predicates and equality

  • null
  • atom
  • listp
  • consp
  • endp
  • numberp
  • integerp
  • rationalp
  • floatp
  • symbolp
  • keywordp
  • stringp
  • functionp
  • zerop
  • plusp
  • minusp
  • evenp
  • oddp
  • eq
  • eql
  • equal

EQ: the same object; EQL: also numbers of the same type and value; EQUAL: also lists and strings with equal contents.

(atom '(a))
returns NIL
(eq '(a) '(a))
returns NIL
(equal '(a) '(a))
returns T

Numbers

  • +
  • -
  • *
  • /
  • =
  • /=
  • <
  • >
  • <=
  • >=
  • 1+
  • 1-
  • mod
  • rem
  • max
  • min
  • abs
  • sqrt
  • expt
  • floor
  • ceiling
  • truncate
  • round
  • float
  • gcd

FLOOR, CEILING, TRUNCATE and ROUND return their first value (the quotient).

(/ 7 2)
returns 7/2
(/ 7.0 2)
returns 3.5
(floor 7 2)
returns 3
(mod -7 3)
returns 2
(expt 2 64)
returns 18446744073709551616

Functions as values

  • function
  • funcall
  • apply
  • mapcar
  • reduce
  • remove-if
  • remove-if-not
  • every
  • some
  • identity
  • eval
(mapcar #'(lambda (x) (* 2 x)) '(1 2 3))
returns (2 4 6)
(funcall #'+ 1 2)
returns 3
(apply #'+ 1 '(2 3))
returns 6
(remove-if #'oddp '(1 2 3 4))
returns (2 4)

Output and tracing

  • print
  • prin1
  • princ
  • terpri
  • fresh-line
  • format
  • trace
  • untrace

FORMAT takes T (write the text) or NIL (return it as a string) and the directives ~A ~S ~D ~% ~& ~~. (trace f) records calls to F even with the trace switch off.

(format nil "~a is ~d" 'x 5)
returns "X is 5"
(print 42)
returns 42