Tuesday, 29 January 2013

Exercise 194: Identify the values among the following expressions:

Exercise 194: Assume the definitions area in DrRacket contains (define (f x) x). Identify the values among the following expressions:
  1. (cons f empty)
  2. (f f)
  3. (cons f (cons 10 (cons (f 10) empty)))
Explain why they are values and why the remaining expressions are not values.
Answer

I'm not completely confident on this answer - my understanding is that a value is being used in an expression, and a function will be using one (or more or less) values as input.

1.  (cons f empty)

In this case f and empty are values. They are being operated on by the cons.

2. (f f)

In this case the first f is a function, and the second f is a value - it is being operated on f

3. (cons f (cons 10 (cons (f 10) empty)))

In this case all the cons's are functions.  The first f is a value (being operated on by the first cons). The second f is a function (operating on the 10). The remaining 10 and empty are simple values.

No comments:

Post a Comment