Discussion:
[Lisp-cn] sicp 练习2.4如果用common lisp写需要怎么实现?
savior michael
2013-12-17 05:28:04 UTC
Permalink
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£

ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º



;; ex 2.4. Alternate procedural rep of pairs.
;; given:
(define (cons a b)
(lambda (m) (m a b)))

;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))


;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))



;; Usage: (define x (cons 3 4))
(car x)
(cdr x)




ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
Xiaofeng Yang
2013-12-17 14:22:38 UTC
Permalink
;; -*- mode: common-lisp; coding: utf-8 -*-
(in-package :cl-user)

;; (define (cons a b)
;; (lambda (m) (m a b)))

(defun my-cons (a b)
(lambda (m) (funcall m a b)))

;; (define (car z)
;; (z (lambda (p q) p)))
(defun my-car (z)
(funcall z (lambda (p q) p)))

;; (define (cdr z)
;; (z (lambda (p q) q)))
(defun my-cdr (z)
(funcall z (lambda (p q) q)))


µ÷Óãº
(setq x (my-cons 3 4))
(my-car x)
(my-cdr x)


Ãû×ÖÓÃmy-car, my-cdr, my-consÊÇΪÁ˱ÜÃâºÍ±ê׌¿âÀïÃæµÄº¯ÊýͬÃû¡£


Best regards,
Xiaofeng Yang
Post by savior michael
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£
ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º
;; ex 2.4. Alternate procedural rep of pairs.
(define (cons a b)
(lambda (m) (m a b)))
;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))
;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))
;; Usage: (define x (cons 3 4))
(car x)
(cdr x)
ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
savior michael
2013-12-18 06:19:16 UTC
Permalink
·Ç³£žÐл£¬ÎÒÉÙÔÚmy-consʹÓÃfuncall£¬µŒÖÂÔËÐÐŽíÎó£¬È»ºó¶ÔlambdaµÄÀíœâŸÍÓÐÁËÆ«²î£¬µœ×îºó¶Œ¿ªÊŒÐŽºêÀŽÊµÏÖÁË¡£»¹ÒÔΪschemeµÄlambdaºÍclÖеÄÓвî±ðÄØ¡£ºÃÈõ°¡¡£
Post by Xiaofeng Yang
;; -*- mode: common-lisp; coding: utf-8 -*-
(in-package :cl-user)
;; (define (cons a b)
;; (lambda (m) (m a b)))
(defun my-cons (a b)
(lambda (m) (funcall m a b)))
;; (define (car z)
;; (z (lambda (p q) p)))
(defun my-car (z)
(funcall z (lambda (p q) p)))
;; (define (cdr z)
;; (z (lambda (p q) q)))
(defun my-cdr (z)
(funcall z (lambda (p q) q)))
µ÷Óãº
(setq x (my-cons 3 4))
(my-car x)
(my-cdr x)
Ãû×ÖÓÃmy-car, my-cdr, my-consÊÇΪÁ˱ÜÃâºÍ±ê׌¿âÀïÃæµÄº¯ÊýͬÃû¡£
Best regards,
Xiaofeng Yang
ÐŽµÀ£º
Post by savior michael
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£
ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º
;; ex 2.4. Alternate procedural rep of pairs.
(define (cons a b)
(lambda (m) (m a b)))
;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))
;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))
;; Usage: (define x (cons 3 4))
(car x)
(cdr x)
ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
Xiaofeng Yang
2013-12-18 09:35:48 UTC
Permalink
¿Ï¶šÓвî±ðµÄ¡£Äã²»ÄÜ°ÑËûÃǵ±³ÉÊÇÒ»ÑùµÄ¡£

Best regards,
Xiaofeng Yang
Post by savior michael
·Ç³£žÐл£¬ÎÒÉÙÔÚmy-consʹÓÃfuncall£¬µŒÖÂÔËÐÐŽíÎó£¬È»ºó¶ÔlambdaµÄÀíœâŸÍÓÐÁËÆ«²î£¬µœ×îºó¶Œ¿ªÊŒÐŽºêÀŽÊµÏÖÁË¡£»¹ÒÔΪschemeµÄlambdaºÍclÖеÄÓвî±ðÄØ¡£ºÃÈõ°¡¡£
Post by Xiaofeng Yang
;; -*- mode: common-lisp; coding: utf-8 -*-
(in-package :cl-user)
;; (define (cons a b)
;; (lambda (m) (m a b)))
(defun my-cons (a b)
(lambda (m) (funcall m a b)))
;; (define (car z)
;; (z (lambda (p q) p)))
(defun my-car (z)
(funcall z (lambda (p q) p)))
;; (define (cdr z)
;; (z (lambda (p q) q)))
(defun my-cdr (z)
(funcall z (lambda (p q) q)))
µ÷Óãº
(setq x (my-cons 3 4))
(my-car x)
(my-cdr x)
Ãû×ÖÓÃmy-car, my-cdr, my-consÊÇΪÁ˱ÜÃâºÍ±ê׌¿âÀïÃæµÄº¯ÊýͬÃû¡£
Best regards,
Xiaofeng Yang
Post by savior michael
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£
ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º
;; ex 2.4. Alternate procedural rep of pairs.
(define (cons a b)
(lambda (m) (m a b)))
;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))
;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))
;; Usage: (define x (cons 3 4))
(car x)
(cdr x)
ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
Liutos
2013-12-19 01:30:43 UTC
Permalink
²î±ð»¹ŽóµÃºÜ£¬Ã÷ÏԵğÍÊDzÎÊýÁбíÖ§³ÖµÄ¶«Î÷µÄ²î±ðXD
Post by Xiaofeng Yang
¿Ï¶šÓвî±ðµÄ¡£Äã²»ÄÜ°ÑËûÃǵ±³ÉÊÇÒ»ÑùµÄ¡£
Best regards,
Xiaofeng Yang
Post by savior michael
·Ç³£žÐл£¬ÎÒÉÙÔÚmy-consʹÓÃfuncall£¬µŒÖÂÔËÐÐŽíÎó£¬È»ºó¶ÔlambdaµÄÀíœâŸÍÓÐÁËÆ«²î£¬µœ×îºó¶Œ¿ªÊŒÐŽºêÀŽÊµÏÖÁË¡£»¹ÒÔΪschemeµÄlambdaºÍclÖеÄÓвî±ðÄØ¡£ºÃÈõ°¡¡£
Post by Xiaofeng Yang
;; -*- mode: common-lisp; coding: utf-8 -*-
(in-package :cl-user)
;; (define (cons a b)
;; (lambda (m) (m a b)))
(defun my-cons (a b)
(lambda (m) (funcall m a b)))
;; (define (car z)
;; (z (lambda (p q) p)))
(defun my-car (z)
(funcall z (lambda (p q) p)))
;; (define (cdr z)
;; (z (lambda (p q) q)))
(defun my-cdr (z)
(funcall z (lambda (p q) q)))
µ÷Óãº
(setq x (my-cons 3 4))
(my-car x)
(my-cdr x)
Ãû×ÖÓÃmy-car, my-cdr, my-consÊÇΪÁ˱ÜÃâºÍ±ê׌¿âÀïÃæµÄº¯ÊýͬÃû¡£
Best regards,
Xiaofeng Yang
Post by savior michael
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£
ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º
;; ex 2.4. Alternate procedural rep of pairs.
(define (cons a b)
(lambda (m) (m a b)))
;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))
;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))
;; Usage: (define x (cons 3 4))
(car x)
(cdr x)
ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn
---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
--
Liutos Love Linux LaTeX Lisp Ling

ÎÒµÄGitHubÖ÷Ò³£ºhttps://github.com/Liutos
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
Mac David
2013-12-17 11:59:29 UTC
Permalink
ÎÒ¿ŽÎÒÕâÑùÀíœâµÄ¶Ô²»¶Ô :-)

(defun my-cons (a b)
#'(lambda (m) (funcall m a b)))

(defun my-car (z)
(funcall z #'(lambda (p q) p)))

(defun my-cdr (z)
(funcall z #'(lambda (p q) q)))
Post by savior michael
ÕâžöÏ°ÌâÊÇÏëʹÓÃlambdaÀŽÊµÏÖcons »¹ÓÐcar cdr.ÎÒ¿ŽÁ˺ßòÅÃ÷°×ÒâÍŒ¡£ÌâÄ¿ÉÏûÓОø³öÏêÏžµÄœâÊÍ¡£
ÊÇ¿ŽÁËŽð°žÖ®ºó²ÅÃ÷°×ÕâÁœžöº¯ÊýÊÇÈçºÎÔËÐеġ£Žð°žÈçÏ£º
;; ex 2.4. Alternate procedural rep of pairs.
(define (cons a b)
(lambda (m) (m a b)))
;; Commentary: cons returns a function that takes a function of 2 ;; args, a and b. The function will receive the values of a and b ;; passed to cons when cons was called initially.
;; z is a function that takes a 2-arg function. That inner function ;; will be passed p and q in that order, so just return the first arg, p. (define (car z)
(z (lambda (p q) p)))
;; ... so this is obvious. (define (cdr z)
(z (lambda (p q) q)))
;; Usage: (define x (cons 3 4))
(car x)
(cdr x)
ÎÒÏëžÄ³Écommon lispµÄʵÏÖ£¬ÐŽÁË°ëÌìҲûÓÐÐŽ³öÀŽ¡£ÇóœÌŽóÉñÖžµã¡£
--
--
Lisp-cn(LispÖÐÎÄÓû§×é)
CLUG http://lisp.org.cn

---
ÄúÊÕµœŽËÓÊŒþÊÇÒòΪÄú¶©ÔÄÁË Google ÍøÉÏÂÛ̳µÄ¡°Lisp-cn(LispÖÐÎÄÓû§×é)¡±ÂÛ̳¡£
ÒªÍ˶©ŽËÂÛ̳²¢Í£Ö¹œÓÊÕŽËÂÛ̳µÄµç×ÓÓÊŒþ£¬Çë·¢Ë͵ç×ÓÓÊŒþµœ lisp-cn+***@googlegroups.com¡£
Òª²é¿Žžü¶àÑ¡ÏÇë·ÃÎÊ https://groups.google.com/groups/opt_out¡£
Loading...