Penguin
Note: You are viewing an old revision of this page. View the current version.
This is a list
(apple orange banana)
Lists can be nested
((fruit (apple orange banana)) (colour (red green)))
Lisp usually interprets lists as function calls
(+ 40 2) 42
Lisp usually interprets symbols (i.e. identifiers) as variable references
(* pi 2) 6.2831853071795862
A ' sign "quotes" a something, i.e. prevents Lisp from interpreting it. So
'(elem1 elem2 elem3)

is a list of symbols.

let creates a list of variables that can be used in an expression
(let ((a 1) (b 2) (mylist '(a b))) expression)

in expression a = 1, b = 2, mylist = '(a b).

setq changes the value of a variable.

(setq mylist '(a b c))

The cons function makes a pair
(cons 'first 42)

Cons pairs are usually used to make lists: the first element is the first element on the list; the second element is either a cons pair for the rest of the list or a nil to mark the end of the list.

'(1 2 3)

is shorthand for
(cons 1 (cons 2 (cons 3 nil)))
car returns the first element of a list

(print (car mylist))

a

cdr returns the rest of the list (ie all-but-first)
(print (cdr mylist)) (b c)

And that is basically LISP ;) The names "car" and "cdr" are because the first machine that lisp was written for, there were 2 registers and the commands "contents of address register" and "contents of decrement register".

The following example is an inefficient factorial function1?

(defun fact (x) ;a recursive function

(if (> x 0)

(* x (fact (- x 1))) 1

) )


1? this stupid function has become the HelloWorld of functional ProgrammingLanguages?, for some reason.

  • specific

lib/BlockParser.php:505: Notice: Undefined property: _tight_top