티스토리 툴바

Programming Language

'Programming Language/LISP'에 해당되는 글 9건

  1. 2009/05/16 &rest
  2. 2009/05/16 destructuring-bind
  3. 2009/05/16 Parameter Lists
  4. 2009/05/16 mapcar
  5. 2009/05/10 LISP 어휘
  6. 2009/05/10 Iteration dotimes
  7. 2009/04/28 Iteration do (10)
  8. 2009/04/21 Your Kitten of Death awaits (2)
  9. 2008/09/18 윈도우 XP 에서 리스프(LISP) 환경 구축

&rest

2009/05/16 15:40 : Programming Language/LISP
CL-USER> (defun our-compose (&rest fns)
       (reverse fns))
OUR-COMPOSE
CL-USER> (our-compose #'list #'round #'sqrt)
(#<SYSTEM-FUNCTION SQRT> #<SYSTEM-FUNCTION ROUND> #<SYSTEM-FUNCTION LIST>)
CL-USER> (reverse '(1 2 3))
(3 2 1)
CL-USER> (reverse #'list #'sqrt)
; Evaluation aborted.
CL-USER> (reverse '(#'list #'sqrt))
(#'SQRT #'LIST)
CL-USER> (reverse '(list sqrt))
(SQRT LIST)
CL-USER>
Posted by 창민짱 Trackback 0 Comment 0
(destructuring-bind (x y z) (list 1 2 3)
(list :x x :y y :z z)) ==> (:X 1 :Y 2 :Z 3)

(destructuring-bind (x y z) (list 1 (list 2 20) 3)
(list :x x :y y :z z)) ==> (:X 1 :Y (2 20) :Z 3)

(destructuring-bind (x (y1 y2) z) (list 1 (list 2 20) 3)
(list :x x :y1 y1 :y2 y2 :z z)) ==> (:X 1 :Y1 2 :Y2 20 :Z 3)

(destructuring-bind (x (y1 &optional y2) z) (list 1 (list 2 20) 3)
(list :x x :y1 y1 :y2 y2 :z z)) ==> (:X 1 :Y1 2 :Y2 20 :Z 3)

(destructuring-bind (x (y1 &optional y2) z) (list 1 (list 2) 3)
(list :x x :y1 y1 :y2 y2 :z z)) ==> (:X 1 :Y1 2 :Y2 NIL :Z 3)

(destructuring-bind (&key x y z) (list :x 1 :y 2 :z 3)
(list :x x :y y :z z)) ==> (:X 1 :Y 2 :Z 3)

(destructuring-bind (&key x y z) (list :z 1 :y 2 :x 3)
(list :x x :y y :z z)) ==> (:X 3 :Y 2 :Z 1)

(destructuring-bind ((&key w x) &rest y) '((:w 3) a)
(list w x y))
(3 NIL (A))
&key 에 의해서 w 는 3 이되고
x 는 nil
y 는 a 지만 &rest 에 의해서 리스트화된다.

아래의 2개의 결과가 같다는것을 참고.

CL-USER> (destructuring-bind (x y . z) '(1 2 3)
    (list x y z))
(1 2 (3))

CL-USER> (destructuring-bind (x y &rest z) '(1 2 3)
    (list x y z))
(1 2 (3))


Posted by 창민짱 Trackback 0 Comment 0
lisp 에서 자주사용하는 #'+ 함수는 어떠한 수의 arguments 라도 취할수 있다.
만약 우리가 이러한 함수를 작성하고 싶다면 rest parameter 를 사용하면 된다.

만약 함수의 parameter list안의 마지막 변수 앞에 &rest 를 넣어주면 함수가 호출이 되었을때 이 변수는 리스트화 되어 설정된다.

(defun our-funcall (fn &rest args)
   (apply fn args))


Posted by 창민짱 Trackback 0 Comment 0

mapcar

2009/05/16 14:10 : Programming Language/LISP
CL-USER> (mapcar #'+ '(1 2 3 4) '(5 6 7 8) '(9 0))
(15 8)
CL-USER> (mapcar #'+ '(3) '(4) '(5))
(12)
CL-USER> (mapcar #'list
         '(a b c)
         '(1 2 3 4))
((A 1) (B 2) (C 3))

mapcar 는 함수와 한개또는 그 이상의 리스트를 취한다.
리턴값은 각 리스트로부터 취한 각 요소에 함수를 적용한 결과값이다.
first argument가 함수가 되겠고
나머지 argument 는 한개 이상의 리스트가 되겠다.

Posted by 창민짱 Trackback 0 Comment 0
함수가 정의된 밖의 변수를 참조할 때 이 변수를 free variable 이라고 부른다.
이때의 함수를 closure 라 부른다.

예를 들어 코드로 설명하면
CL-USER> (setf fn (let ((i 3))
            #'(lambda (x) (+ x i))))
#<FUNCTION :LAMBDA (X) (+ X I)>

i 는 free variable 이 되고 이때의 람다 함수를 closure 라 한다.
Posted by 창민짱 Trackback 0 Comment 0
CL-USER> (dotimes (i 10 100)
       (format 't "dotimes ~A!!!~%" i))
     
dotimes 0!!!
dotimes 1!!!
dotimes 2!!!
dotimes 3!!!
dotimes 4!!!
dotimes 5!!!
dotimes 6!!!
dotimes 7!!!
dotimes 8!!!
dotimes 9!!!
100
CL-USER>

10번 반복한다.
i 에는 0 부터 9 까지의 숫자가 들어가고
dotimes 의 3번째 argument 가 리턴된다.
Posted by 창민짱 Trackback 0 Comment 0

; SLIME 2005-12-27
CL-USER> (defun show-squares (start end)
    (do ((i start (+ i 1))) => first argument
        ((> i end) 'done) => second argument
      (format t "~A ~A~%" i (* i i))))
SHOW-SQUARES
CL-USER> (show-squares 2 5)
2 4
3 9
4 16
5 25
DONE
CL-USER>

(i start (+ i 1) => first argument 이고 (variable initial update) 순서이다.
((> i end) 'done) => second argument 이고 test expression (> i end) 가 참이면 나머지 표현들은 평가된다.
그리고 마지막 값은 do 의 return 값이 된다. 여기 예제에서는 'done 이 return 값이다.
남아 있는 arguments 는 loop 의 body 가 된다. 각 반복때마다 순서대로 평가된다.

Posted by 창민짱 Trackback 0 Comment 10
Samuel 님의 블로그를 참고하여 eclipse 3.4 에 LISP 플러그인을 설치했다.
emacs 를 통해 LISP 를 접해오는 나에게 이런 정보를 제공해 주신 Samuel 님에게 감사드린다.
이름하여 CUSP.
간단히 10초만에 짜본 hello world 프로그램이다.


무엇보다 맘에드는것은 lisp 언어로 짜여진 binary executable 이 생성 가능하다는 것이다.


근데 디버거랑 컴파일러가 전체가 포함된 관계로 20메가 이상의 파일이 생성된다.
저작자 표시 비영리
Posted by 창민짱 Trackback 0 Comment 2
윈도우에서 리스프 개발 환경을 구성해 보자.

1시간만에 완성.ㅋㅋ


참고 사이트 :
Posted by 창민짱 Trackback 0 Comment 0