简体   繁体   English

方案中的清单程序

[英]list procedure in Scheme

I am studying for an exam right now but I am not sure if I understood the list procedure in Scheme. 我正在学习考试,但我不确定我是否理解Scheme中的list程序。 I know that it can be used for creating list variables like (define x (list 'a 'b 'c)) . 我知道它可以用于创建列表变量,如(define x (list 'a 'b 'c)) However I saw another usage of it in procedure creation: 但是我在程序创建中看到了它的另一种用法:

1 ]=> (define foo3
           (lambda (b lst)
                (if b
                    (car lst)
                    (cadr lst)
                )
           )
      )
;Value: foo3

1 ]=> (foo3 #f ’(a b))
;Value: b

1 ]=> ((foo3 #t (list cdr car)) ’(a b c))
;Value: (b c)

What does the (list cdr car) mean? (列出cdr汽车)是什么意思? (I know what cdr and car means in terms of referencing first and rest of the list) (我知道cdrcar在引用第一个和其余列表方面意味着什么)

In the code, (list cdr car) is just a list of procedures. 在代码中, (list cdr car)只是一个程序列表。 foo3 will select one procedure from that list, according to the passed parameter b . foo3将根据传递的参数b从该列表中选择一个过程。 In the second example, this snippet: 在第二个示例中,此代码段:

(foo3 #t (list cdr car))

... Will return cdr because the first parameter was #t , so in the end we're just evaluating this: ...将返回cdr因为第一个参数是#t ,所以最后我们只评估这个:

(cdr '(a b c))
=> '(b c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM