Home Blogs Books NewTab rsrc

Learn Lisp with me - 2

programming lisp

Creating an alist - association list

(defparameter *nodes* '((living-room (you are in the living-room.
a wizard is snoring loudly on the couch.))
(garden (you are in a beautiful garden.
there is a well in front of you.))
(attic (you are in the attic.
there is a giant welding torch in the corner.))))

Function mapping with mapcar

(mapcar #'describe-path '((GARDEN WEST DOOR) (ATTIC UPSTAIRS LADDER)))

((THERE IS A DOOR GOING WEST FROM HERE.) (THERE IS A LADDER GOING UPSTAIRS FROM HERE.))

mapcar takes one function and maps it to every member of the list.

Functions that take other functions as parameters, such as mapcar , are very useful and a distinguishing feature of Lisp. Such functions are called higher- order functions.

(mapcar #'car '((foo bar) (baz qux)))
is the same as
(mapcar (function car) '((foo bar) (baz qux)))

(foo baz)

The append function

(append '(mary had) '(a) '(little lamb))

(MARY HAD A LITTLE LAMB)

The apply function

(apply #'append '((THERE IS A DOOR GOING WEST FROM HERE.)
(THERE IS A LADDER GOING UPSTAIRS FROM HERE.)))

(THERE IS A DOOR GOING WEST FROM HERE. THERE IS A LADDER GOING UPSTAIRS FROM HERE.)

The find function

(find 'y '((5 x) (3 y) (7 z)) :key #'cadr)

(3 Y)

push, but gently

(defparameter *foo* '(1 2 3))

FOO

(push 7 *foo*)

(7 1 2 3)

*foo*

(7 1 2 3)

Are you even a member?

It's still not over. Stay tuned for updates.


Learn Lisp with me
C coding styles