1

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)

hevele
  • 903
  • 6
  • 20
  • 38

1 Answers1

3

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)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Ow, I see. I am still thinking the way that list are basically arrays in C. I didn't think that we can store procedures in it. Thank you very much. – hevele May 01 '13 at 17:38
  • Yes, that's one of the nice things in functional programming languages: procedures are just like any other data type, and you can pass them around as parameters, return them from other procedures, store them in data structures, etc. – Óscar López May 01 '13 at 17:39
  • For what it's worth, you can store functions in C arrays too, it's just not very common because C's functions are such a hassle to create. – amalloy May 01 '13 at 20:23