-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp5.lisp
42 lines (26 loc) · 911 Bytes
/
lisp5.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
;; Numbers, T and NIL evaluate to themselves
;; Defun is a special kind of function, called macro function
(defun cube (n)
(* (* n n) n))
(cube 3)
(defun pythago (x y)
(sqrt (+ (* x x) (* y y))))
(pythago 3 4)
(defun milles-per-gallon (init-odo fin-odo gal-consu)
(/ (- fin-odo init-odo) gal-consu))
(milles-per-gallon 100 200 15)
;; Symbols vs variables: a symbol evaluates to the value of the variable it refers to
(defparameter salvi 2)
(defparameter angy 2)
(equal salvi angy) ; evaluate the values
(equal 'salvi 'angy) ; comparing the symbols themselves
;; A quoted object evaluates ti the object itself, without the quote
(+ (length '(1 foo 2 moo))
(third '(1 foo 2 moo)))
(defun longer-than (xs ys)
(> (length xs) (length ys)))
(longer-than '(a b c d) '(a b y))
(defun add-length (xs)
(cons (length xs) xs))
(add-length '(moo goo gai pan))
(add-length (add-length '(a b c)))