I am studying for an exam right now but I am not sure if I understood the list
procedure in Scheme. I know that it can be used for creating list variables like (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? (I know what cdr
and car
means in terms of referencing first and rest of the list)
In the code, (list cdr car)
is just a list of procedures. foo3
will select one procedure from that list, according to the passed parameter 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 '(a b c))
=> '(b c)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.