Introduction to lisp

BASIC CONCEPTS

Atom

3 types of Atom

  • Symbols: (case insensitive) John, abc, 23-Jordan
  • Numbers: 123, 0, #C(3 4) ;complex number = 3+4i
    #C(3/4 4) ;complex number = 0.75+4i != 3/4+4i
  • Constants(self-evaluating): NIL, T

Symbolic-Expression(s-expression)

  • An atom is an s-expression
  • If s1, s2, ..., sn are s-expression, so is the list(s1, s2, ..., sn)

List

A nonatomic s-expression
A collection of atom or list enclosed by parentheses ()

NIL and T

NIL

A special list called the null list
Also an atom
non-NIL is true

T

All the data types are subtypes of T

EVALUATION

Form

A form is an s-expression that is intended to be evaluated
e.g. (+ 2 4)

Evaluation

Evaluate the second through the last arguments(recursion)

Control the Flow of Evaluation

Quote

Return the arguments without evaluation

/> 'a
A
/> (quote a)
A

EVAL

tell the interpreter to evaluate the expression

/> (eval '(* 2 3))
6

Binding Variables Explicity

Set binds a value to the global environment
Setq is a variant of SET which dose not evaluate the first argument

/> (set 'index (+ 1 4))
5
/> (set 'index (+ 1 index))
6
/> (setq index (+ 2 index)) ;no ' is needed for SETQ
8

setf is a macro that examines an access from and produces a call to the corresponding update function
It uses its first argument to obtain a memory location and places the calue of the second argument in that location

/> (setf index (+ 2 6))
8
/> (setq a '(1 2 3)) ;create a list of '(1 2 3)
(1 2 3)
/> (setf (car a) 4) ;car is a function that returns the head of the list
4
/> a
(4 2 3)

setq and setf

source: https://blog.csdn.net/csfreebird/article/details/8761634
(Oct 18 2018)
setq: 避免使用set时用'来阻止表达式求值
setf: 用于设置list或array

/> (setq L '(a b (c d) e f g))
(a b (c d) e f g)
/> L
(a b (c d) e f g)
/> (setf (L 1) 'B)
B
/> L
(a B (c d) e f g)
/> (L 1)
B

LIST MANIPULATION

Cons cell: A two-field record

/> (cons 3 nil)
(3)
/> (cons 3 (cons 4 'x))
(3 4 . x) ;dotted-pair notation, improper list
/> (cons 3 (cons 4 (cons 'x nil)))
(3 4 x)

C[AD][AD]*R and NTHCDR

/> (car NIL)
NIL ;behave as an atom
/> (cdr NIL)
NIL ;behave as a list

/> (cadr '(a b c)) ;(car (cdr '(a b c)))
B
/>(cdar '((1 . 2) 3))
2

/> (nthcdr 0 '(1 2 3 4 5))
(1 2 3 4 5)
/> (nthcdr 3 '(1 2 3 4 5)) ;(cdr cdr( cdr '(1 2 3 4 5)))
(4 5)

Lists: implemented on top of cons pairs

LIST

/> (list 3 4 'x)
(3 4 X)
/> '(3 4 x)
/> (list 3 (+ 2 2) x)
(3 4 X)

/> (setq L (list 1 2 3 4))
(1 2 3 4)
/> (car L)
1
/> (cdr L)
(2 3 4)

PUSH and POP

/> (setq a nil)
NIL
/> (push 3 a)
(3)
/> (push 5 a)
(5 3) ;the leftmost is the top of the stack
/> (pop a)
5
/> a
(3)
/> (pop a)
3
/> (pop a)
NIL
/> a
NIL

NTH, APPEND, DELETE

/> (nth 5 '(a b c d e f))
F

APPEND concatenates lists into one list. It created a new list and does NOT modify the argument lists
/> (append '(a b) 'c)
(A B . C)

/> (delete 'b '(a b c))
(A C)
/> (delete 'd '(a b c)) ;no error message
(A B C)
/> (delete 'a '(a a a b a))
(B)

Type Predicates

TYPOP object data_type: is the type of object data_type
SUBTYPEP subtype data_type: is the subtype a subtype of data_type

  • ATOM: atom
    SYMBOLP: symbol
    CONSP: cons
    LISTP: list
    /> (symbolp 10)
    NIL
    /> (atom 10)
    T
    /> (typep 10 'atom)
    T ;same as the last result

Equality Predicates

  • (= x y): Numbers only and ignore type
  • (EQ x y): Are the two objects implementationally identical?
    Same memory location
  • (EQL x y): Are the two objects conceptually identical?
  • (EQUAL x y): isomorphic?

/> (setq q1 '(abc 123))
(ABC 123)
/> (setq q2 '(abc 123))
(ABC 123)
/> (eq q1 q2)
NIL
/> (eql q1 q2)
NIL
/> (eql (car q1) (car q2))
T
/> (equal q1 q2)
T

Conditional Constructs

AND NOT OR

IF test then [else]

/> (setq p 10)
10
/> (set1 d 3)
3
/> (if (eql (mod p d) 0)
(list '* p d)
(list '+ (list '* d (floor (/ p d))) (mod p d)))
(+ (* 3 3) 1)

COND {(test {form})}

/> (setq person 'Sam)
SAM
/> (cond
((eql person 'Peter) 'Boss)
((eql person 'Sam) 'Staff1001)
((eql person 'Paco) 'Staff1002)
(t 'Intruder!))
STAFF1001

Indefinite Iteration

LOOP {forms}

/> (setq x 3)
/> (loop
(print x)
(setq x (- x 1))
(if (zerop x) (return "GO!")))
3
2
1
"GO!"

DO

(DO({ (variable [initial-value [step-form]]) })
(end-test {result})
{declaration}
{tag | statement})
/> (setq x 3)
/> (do ((a x (- a 1)))
((zerop a) "GO!")
(print a))

Let

LET ({var | (car value)}) {declaration} {from}
/> (let ((x 3))
(loop (print x))
(setq x (- x 1))
(if (zerop x) (return "GO!")))
3
2
1
"GO!"

YOUR CONTROL CONSTRUCTS

Named Function

DEFUN name lambda-list {declaration | doc-string} {form}
/> (defun decompose (p d)
(if (eql (mod p d) 0)
(list '* p d)
(list '+ (list '* d (floor (/ p d))) (mod p d))))
DECOMPOSE
/> (decompose 10 4)
(+ (* 4 2) 2)

Lambda Expression

LAMBDA lambda-list . body

/> ((lambda (p d)
(if (eql (mod p d) 0)
(list '* p d)
(list '+ (list '* d (floor (/ p d))) (mod p d)))) 10 4)
(+ (* 4 2) 2)

你可能感兴趣的:(Introduction to lisp)