Blog

2013.11.06 Lisp 풀이

November 6, 2013

2013.11.06 Lisp 풀이

Lisp 99 Examples

Link

Load

(load "test") ;; load test.lisp

그러나, test.lisp 에서 아래와 같은 코드를 작성하면, REPL 에서 print 가 정의되지 않았다고 실행되지 않는다.

(defun hello()
    (print 3))

Couting Closest Nodes Problem

(defun get-node-count(node)
       (if (car node)
           (list (cons (car (car node)) (length (cdr node))) (get-node-count (cdr node)))))

(define *graph*
         '((a . (b d))
           (b . (c e))))

CL-USER> (get-node-count *graph*)
((A . 1) ((B . 0) NIL))

왜 1, 0이 나오지? 2, 2가 나와야 하는거 아닌가.

Emacs Slime 으로 디버깅 하는법좀 익혀야겠다. ㅠㅠ 아 할일이 많다.

해결!

(defun get-node-count (node)
	   (if (car node)
	       (cons (cons (caar node) (length (cadar node))) (get-node-count (cdr node)))
	       '()))

CL-USER> *graph*
((A (B D)) (B (C E)))
CL-USER> (get-node-count *graph*)
((A . 2) (B . 2))
CL-USER> 

((a b)) 이런식으로 되어있어서, car로 못얻어오고 caar 로 얻어와야 했음.

list 함수는 nil 을 받아 그것을 리스트 원소로 만드는데 비해서, cons 함수는 nil 을 무시함.

Array