난해한(!) 명령어중에 하나로 car / cdr이라는 lisp 명령어가 있다.
car은 first에 대응되며 list의 가장 처음 내용만 보여주고
cdr은 rest에 대응되며 list의 처음것을 제외한 내용을 보여준다.
1> (car '(1 2 3))
1
1> (cdr '(1 2 3))
(2 3)
1> (rest '(1 2 3))
(2 3)
return the car of a list node
(car <expr>)
May be used as a place form.
<expr>the list node
returnsthe CAR of the list node
return the cdr of a list node
(cdr <expr>)
May be used as a place form.
<expr>the list node
returnsthe CDR of the list node
all cxxr combinations
(cxxr <expr>)
all cxxxr combinations
(cxxxr <expr>)
all cxxxxr combinations
(cxxxxr <expr>)
May be used as place forms when common2.lsp loaded.
a synonym for car
(first <expr>)
a synonym for cadr
(second <expr>)
a synonym for caddr
(third <expr>)
a synonym for cadddr
(fourth <expr>)
fifth list element
(fifth <expr>)
sixth list element
(sixth <expr>)
seventh list element
(seventh <expr>)
eighth list element
(eighth <expr>)
ninth list element
(ninth <expr>)
tenth list element
(tenth <expr>)
a synonym for cdr
(rest <expr>)
May be used as place forms when common2.lsp loaded. fifth through tenth defined in common2.lsp.
Etymology
Lisp was originally implemented on the IBM 704 computer, in the late 1950s. The 704 hardware had special support for splitting a 36-bit machine word into four parts, an "address part" and "decrement part" of 15 bits each and a "prefix part" and "tag part" of three bits each.
Precursors to Lisp included functions:
car (short for "Contents of the Address part of Register number"),
cdr ("Contents of the Decrement part of Register number"),
cpr ("Contents of the Prefix part of Register number"), and
ctr ("Contents of the Tag part of Register number"),
each of which took a machine address as an argument, loaded the corresponding word from memory, and extracted the appropriate bits.
'는 quote(인용)인데
함수적 언어인 lisp에서 함수를 쓰지 않고 list나 atom을 만드는데 사용한다.
> (quote a)
A
> 'a
A
> '(a)
(A)
> (quote (a))
(A)
>'(+ 1 2)
(+ 1 2)
위의 예제와 같이 (quote val)과 'val은 동일하며 atom을 나타낼때 사용된다.
atom으로 쓰려면 'val
list로 쓰려면 '(val)
로 사용하면 된다. 물론 함수역시 list의 atom으로서 인식이 되어질수 있다.
#'는 lisp 문서를 보다가 찾게 된 녀석인데
#' 는 (function val)과 같은 의미이고
함수 포인터라고 하기도 모호하고 아무튼.. 아직은 이해가 잘 안되는 녀석..
APPLY is also a Lisp primitive function.
APPLY takes a function and a list of objects as input. It invokes the specified function with those objects as its inputs. The first argument to APPLY should be quoted with #’ rather than an ordinary quote; #’ is the proper way to quote functions supplied as inputs to other functions. This will be explained in more detail in Chapter 7.
(apply #'+ '(2 3)) ⇒ 5
(apply #’equal '(12 17)) ⇒ nil
The #’ (or ‘‘sharp quote’’) notation is the correct way to quote a function in Common Lisp. If you want to see what the function CONS looks like in your implementation, try the following example in your Lisp:
연휴이고 하니 뜬금없이 LISP 공부중인데 전에도 이해 못했고 이번에도 이해 못하고 있는 lisp -_-
LISP는 LISt Process 의 약자로
이름대로 모든것으로 LIST 에 기반하여 표현하게 되며, 리스트는 () 로 둘러쌓여 표현된다.
이러한 이유로 lisp 소스를 보면 ()가 가득히 둘러쌓여 가독성이 떨어지는 형태가 된다.
또한 리스트에서 계산등은 모두 전위표기법을 사용하게 되며 기본 연산은 다음과 같이 표현된다.
CL-USER>(+ 1 2 ) ; 1 + 2
3
CL-USER> (- 1 2 ) ; 1 - 2
-1
CL-USER> (/ 1 2) ; 1 / 2
1/2
CL-USER> (* 1 2) ; 1 * 2
2
CL-USER> (mod 3 2) ; 3 % 2
1
CL-USER> (log 2) ; log(2)
0.6931472
CL-USER> (sqrt 2) ; sqrt(2)
1.4142135
CL-USER> (< 1 2) ; 1 < 2
T
그리고 기호를 가지는 변수(?)는 setq 명령어를 이용하여 선언한다.
val을 입력하면 해석할 수 없는 변수이기 때문에 에러가 나지만 setq를 통해서 값을 정해주면
val만 입력해도 입력했던 값이 나오게 된다.
CL-USER> val
Invoking restart: Return to sldb level 2.
; Evaluation aborted on #<UNBOUND-VARIABLE #xC7B642E>.
CL-USER> (setq val 100)
100
CL-USER> val
100
CL-USER> (setq str "This is test string")
"This is test string"
CL-USER> str
"This is test string"
아무튼 defvar 라는 키워드를 이용해서 변수를 선언 할수도 있는데 setq와는 다르게 리턴되는 값이 다르다
CL-USER> (defvar loop 10)
LOOP
CL-USER> loop
10
함수는 defun (DEFine + FUNction 인 듯?)을 통해서 선언이 가능하다.
단순하게 3을 리턴하는 함수로 three 라는 것을 만드는 예제이다.
> (defun three () 3)
THREE
> (three)
3
> (+ (three) 1)
4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> three()
error: unbound variable - THREE
> three
error: unbound variable - THREE
> (+ three 1)
error: unbound variable - THREE
인자를 사용하는 함수로 인자는 아래와 같이 사용한다.
CL-USER> (defun add (x y) (+ x y))
ADD
CL-USER> (add 3 4)
7
리스트는 아래와 같이 '을 먼저 찍고 해주면 된다. (아직 이해 부족.. OTL)
물론 리스트도 setq로 선언하여 사용이 가능하다.
> '(1 2 3)
(1 2 3)
> (1 2 3)
error: bad function - 1
'/어포스트로피/apostrophe
'foo는 (quote foo) 와 동일하고 s expression 에서 함수가 아닌 것으로 해석하도록 하는 명령어이다.
어떻게 보면 변수로 인식시킨다고 해야 하려나? replace 라고 된것을 보면 -_-
Creating sets (actually, creating variables) can be done with setf : this creates a set called learned with three members:
(setf learned '(VB C++ LISP))
The apostrophe is uses to designate that something in brackets isn't a function (or an S-expression). Basically, if LISP receives something like (VB C++ LISP) it assumes VB is the name of a function and C++ and LISP are its arguments. The apostrophe says that this isn't a function, it's a list, and can itself be an argument in a function.