cons,car,cdr
今日は、問題2.4をといてみます。
問題2.4は、lispのconsと、carを
(define (cons x y) (lambda (m) (m x y))) (define (car z) (x (lambda (p q) p)))
と定義されているときのcdrはどう定義されるかと問題。
ということで、cons,car,cdrのこの定義で実装
cons x y = \x' -> x' x y car x = x ( \x' y' -> x') cdr x = x ( \x' y' -> y')
で、実行。
$ ghci cons.hs *Main> :l cons.hs Compiling Main ( condcons.hs, interpreted ) Ok, modules loaded: Main. *Main> :t cons cons :: t -> t1 -> (t -> t1 -> t2) -> t2 *Main> :t car car :: ((t1 -> t2 -> t1) -> t) -> t *Main> :t cdr cdr :: ((t1 -> t2 -> t2) -> t) -> t *Main> let test = cons 8 10 *Main> car test 8 *Main> cdr test 10
うまく動いてるようです