Reading — step 1 of 5
Learn
~1 min readFunctions and Lists
Lists are Lisp's core data structure (LISP = LISt Processing). Create with list or '(...):
(list 1 2 3) ; (1 2 3) — function call
'(1 2 3) ; (1 2 3) — quoted, no evaluation
The Big Three operators:
(car list)— first element (head)(cdr list)— rest of list (tail)(cons x list)— prepend x to list
(car '(1 2 3)) ; 1
(cdr '(1 2 3)) ; (2 3)
(cons 0 '(1 2 3)) ; (0 1 2 3)
Alias: first = car, rest = cdr.
Higher-order list operations:
(mapcar #'(lambda (x) (* x 2)) '(1 2 3)) ; (2 4 6)
(remove-if-not #'evenp '(1 2 3 4 5)) ; (2 4)
(reduce #'+ '(1 2 3 4 5)) ; 15
(reduce #'+ '(1 2 3 4 5) :initial-value 100) ; 115
(length '(a b c)) ; 3
(reverse '(1 2 3)) ; (3 2 1)
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…