출처 : http://www.cc.gatech.edu/classes/AY2005/cs3600_fall/resources/lispTutorial.html
Simple Lisp Tutorial
Data Structure
List
Example:
'(a b c d e)
'(1 2 4 5)
'((a b) (c d) (1 3))
Array
Array is a list of demensions.
Examples:
>(setq x
(make-array '(4 3)
:initial-contents
'((a b c)
(1 2 3)
(d e f)
(3 1 2))))
#2A ((a b c) (1 2 3) (d e f) (3 1 2))
>(aref x 0 0)
a
>(setq y (make-array 3 :initial-contents '(a b c)))
#(a b c)
>(setf (aref y 0) 'd)
d
>y
#(d b c)
t and nil
t means true in Lisp.
nil means false in Lisp.
Functions
(functionName arg1 � arg 2)
Functions to control list
[function]: car, cdr
car: return the first element of a list
cdr: return the tail of a list
Example:
>(setq x �(a b c))
(a b c)
>(car x)
a
>(cdr x)
(b c)
[function]: cons
cons is the primitive function to create a new cons whose car is x and whose cdr is y. For example:
Examples:
(cons 'a 'b) => (a . b)
(cons 'a (cons 'b (cons 'c '()))) => (a b c)
(cons 'a '(b c d)) => (a b c d)
[function]: nth
cons returns the nth element of list
Examples:
(nth 0 '(foo bar gack)) => foo
(nth 1 '(foo bar gack)) => bar
(nth 3 '(foo bar gack)) => ()
[function]: list
list constructs and returns a list of its arguments. For example:
Examples:
(list 3 4 'a (car '(b . c)) (+ 6 -2)) => (3 4 a b 4)
(list) => ()
(list (list 'a 'b) (list 'c 'd 'e)) => ((a b) (c d e))
[function]: mapcar
mapcar operates on successive elements of the lists.
Examples:
>(mapcar #'+ '(1 2 3) '(4 5 6))
(5 7 9)
Control Functions� Do, While, Repeat, if, cond
[function]: Do
Example:
>(setq i 3)
>(do ((i 1 (+ 1 i)))
((> i 5))
(print i))
1
2
3
4
5
[function]: if
if test then [else]
Example:
>(setq x 3)
>(if (= x 3) (print "x is equal to 3") (print "x is unequal to 3"))
x is equal to 3
[function]: cond
A cond form has a number (possibly zero) of clauses, which are lists of forms. Each clause consists of a test followed by zero or more consequents.
(cond (test-1 consequent-1-1 consequent-1-2 ...)
(test-2)
(test-3 consequent-3-1 ...)
... )
Example:
>(setq a 5)
>(cond
((evenp a) a) ;if a is even return a
((> a 7) (/ a 2)) ;else if a is bigger than 7 return a/2
((< a 5) (- a 1)) ;else if a is smaller than 5 return a-1
(t 17))
17
Predicate and Equality
(predicate arg)
[function]: null
[function]: listp
[function]: intp
[function]: floatp
Example:
>(setq x 'a)
>(cond
((null x) (print "x is null"))
((listp x) (print "x is a list"))
((atom x) (print "x is an atom"))
((floatp x) (print "x is float")))
x is an atom
[function]: =
= denote numerical euqality.
[function]: eq
eq denote two symbols' equality.
[function]: equal
Two copies of the same list are not eq, but they are equal.
Examples:
> (eq 'a 'a)
T
> (eq 'a 'b)
NIL
> (= 3 3)
T
> (= 3 4)
NIL
> (eq '(a b c) '(a b c))
NIL
> (equal '(a b c) '(a b c))
T
Output functions
[function]: print
[function]: format
Examples:
>(print "hello world")
hello world
>(setq x 2)
(format t "x = ~s ~%" x)
2
functions and misc
[function]: let
A let form can be used to execute a series of forms with specified variables bound to specified values. It can be used to specify the local variables
(let ((var1 value1)
(var2 value2)
...
(varm valuem))
body1
body2
...
bodyn)
Examples:
>(defun lisp-let (a)
(print a)
(let
((a 4))
(print a)))
>(lisp-let 5)
5
4
[function]: progn & prog1
progn{form}* :
progn 구조는 왼쪽에서 오른쪽으로 순서대로 순차적으로 실행하고 숫자의 형태를 가집니다. 마지막 값은 버려지게 됩니다.
prog1 first {form}*:
그에 반해 prog1 progn과 비슷하지만 첫번째 폼에서의 값을 반환합니다.
Examples:
[code]
>(setq x 3)
3
>(progn
(setq x 5)
(+ x 3))
8
>(prog1
(setq x 5)
(+ x 3))
5
[/code]
Self-defined Functions
Example:
>(defun printString (a)
(print t a))
ps
>(printString '(a b c))
(a b c)
Debug Functions
[function] :zoom
[function] :zoom :verbose t
Useful Link
• A one-page summary of the most basic Lisp constructs
• Lisp index reference