在SICP中使用lambda定义cons/car/cdr [英] Use of lambda for cons/car/cdr definition in SICP

查看:222
本文介绍了在SICP中使用lambda定义cons/car/cdr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在SICP中遇到以下关于利弊"和汽车"的替代"定义时,我才开始感到自己对球拍和计划中使用lambda的含糊理解

I was just beginning to feel I had a vague understanding of the use of lambda in racket and scheme when I came across the following 'alternate' definitions for cons and car in SICP

(define (cons x y)
   (lambda (m) (m x y)))

(define (car z)
  (z (lambda (p q) p)))

(define (cdr z)
  (z (lambda (p q) q)))

对于我的一生,我只是无法解析它们.

For the life of me I just cannot parse them.

有人可以解释如何以对所有新手都有意义的方式解析或扩展这些内容吗?

Can anybody explain how to parse or expand these in a way that makes sense for total neophytes?

推荐答案

这是将数据表示为函数的一种有趣方式.请注意, cons的定义返回一个lambda,该关闭 参数xy,在内部捕获它们的值.另请注意,返回的lambda 接收 function m作为参数:

This is an interesting way to represent data: as functions. Notice that this definition of cons returns a lambda which closes over the parameters x and y, capturing their values inside. Also notice that the returned lambda receives a function m as a parameter:

;creates a closure that "remembers' 2 values
(define (cons x y)    (lambda (m) (m x y)))
;recieves a cons holding 2 values, returning the 0th value
(define (car z)       (z (lambda (p q) p)))
;recieves a cons holding 2 values, returning the 1st value
(define (cdr z)       (z (lambda (p q) q)))

在上面的代码中,z是一个闭包,它是由cons创建的,并且在 我们正在将过程主体传递给另一个 lambda作为参数, 还记得m吗?就是这样!它期望的功能.

In the above code z is a closure, the same that was created by cons, and in the body of the procedure we're passing it another lambda as parameter, remember m? it's just that! the function that it was expecting.

理解了上面的内容,很容易看到carcdr的工作方式;让我们 剖析解释器一次评估carcdr的方式:

Understanding the above, it's easy to see how car and cdr work; let's dissect how car, cdr is evaluated by the interpreter one step at a time:

; lets say we started with a closure `cons`, passed in to `car`
(car (cons 1 2))

; the definition of `cons` is substituted in to `(cons 1 2)` resulting in:
(car (lambda (m) (m 1 2)))

; substitute `car` with its definition
((lambda (m) (m 1 2)) (lambda (p q) p))

; replace `m` with the passed parameter
((lambda (p q) p) 1 2)

; bind 1 to `p` and 2 to `q`, return p
1

总结:cons创建一个闭包,以记住"两个值,car 接收封闭件,并将它传递充当用于选择一个功能 第零个值,而cdr用作第一个值的选择器.钥匙 需要理解的是,lambda充当了 关闭. 这有多酷?我们只需要函数来存储和检索任意数据!

To summarize: cons creates a closure that "remembers' two values, car receives that closure and passes it along a function that acts as a selector for the zeroth value, and cdr acts as a selector for the 1st value. The key point to understand here is that lambda acts as a closure. How cool is this? we only need functions to store and retrieve arbitrary data!

car&在大多数情况下,cdr 定义为最多4层 LISP.例如:

Nested Compositions of car & cdr are defined up to 4 deep in most LISPs. example:

(define caddr (lambda (x) (car (cdr (cdr x)))))

这篇关于在SICP中使用lambda定义cons/car/cdr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆