(defun square (x) (* x x))
(square 9.0) yields 81.0
((lambda (x) (* x x)) 9.0) yields 81.0
(defun f (x) (+ x 2)) (defun g (x) (* 3 x))
(defun h (x) (f (g x))
[f, g, h](4) yields (6, 12, 14)
MAP(g, (4, 6, 10)) yields (12, 18, 30)
(A B C) ((A) (B) (C))
(A B . C) (A . B)
-> (cons 'a 'b) (a . b) -> (cons 'a (cons 'b nil)) (a b)
-> (car '(a b)) a
-> (cdr '(a b)) (b)
-> (set 'a '(1 2 3)) ; expands to (set (quote a) (quote (1 2 3))) (1 2 3)
-> (setq a '(1 2 3)) ; setq quotes its first argument (1 2 3)
-> 123 123 -> "Dumb stuff" "Dumb stuff"
-> (setq a '(a b c)) (a b c) -> a (a b c)
-> (setq a '(1 2 3)) (1 2 3) -> (car a) 1 -> (cdr a) (2 3) -> (cdr '(a b c)) (b c)
-> (setq a '(car a)) (car a) -> (eval a) car -> (eval (car (cdr a))) (car a) -> (eval (cons 'car (cons 'a nil))) car
(cond (cond1 exp-list1) ; called a cond clause (cond2 exp-list2) ... (condN exp-listN))
(cond ((null l) nil) ((atom l) (list l)) ((listp l) (append (flatten (car l)) (flatten (cdr l)))))
(defun function_name (formal_parameter_list) expression)
(defun fact (n)
(cond
((eq n 0) 1)
(t (times n (fact (sub1 n))))))
(function_name actual_parameter_list) (defun add2 (x) (add x 2)) -> (add2 1) 3 ((lambda (x) (add x 2)) 1) 3
-> (fact 3) 6
(defun f (x)
(cond
((terminal case) (action))
((inductive case calls f) (action))))
(defun f (x)
(cond
((null x) nil)
(t (g (car x)) (f (cdr x)))))
(defun f (x)
(cond
((atom x) (list x))
(t (append (f (car x)) (f (cdr x))))))
(null x) (atom x) (numberp x) (symbolp x) (listp x) (eq a1 a2) (equal l1 l2)
(and x y) (or x y) (not x)
(list x y) (append x y)
(plus 2 3 4) (minus 4 3) (times 2 3 4) (divide 6 3)
(read)
(print x)
(terpri)
module load franz
% scl
-> (trace function) -> (trace variable) -> (untrace function)
-> (load "foo.l")
^D
(break) or ^C Restart options (Type :C <number>): 0: Provide a value to store as the definition of EXIT 1: Retry getting the definition of EXIT 2: Lisp Top Level
LISP environments typically include a compiler
(help* "keyword") ; help related to the keyword (help 'function) ; help related to function
fun length (l) = if null(l) then 0 else 1 + length(next(l));
fact 0 = 1 fact n = n * fact (n - 1)
fact n = 1, if n = 0 fact n = n * fact (n - 1), if n > 0
sum [] = 0 sum (a:x) = a + sum x product [] = 1 product (a:x) = a * product x
[n * n * n | n <-- [1..]] factors n = [i | i <-- [1..n div 2]; n mod i = 0]
sort [] = [] sort (a:x) = sort [b | b <- x; b <= a] ++ [a] ++ sort [b | b < x; b > a]