Tuesday, 5 February 2013

Exercise 195: Argue why the following sentences are now legal definitions


Exercise 195: Argue why the following sentences are now legal definitions:
  1. (define (f x) (x 10))
  2. (define (f x) (x f))
  3. (define (f x y) (x 'a y 'b))
Explain your reasoning.

1)  (define (f x) (x 10))
We are now allowed to:
  1. include the names of functions and primitive operations in the definition. 
  2. use variables and function parameters in the first position in a definition
In this case the parameter x is a function that will have the value 10 applied to it.  As we can include functions in a function definition this is legal.

2) (define (f x) (x f))
In this case, both x and f are functions. This function will call the function x passing in the value of itself (ie f). As we can include functions in function definitions, this is now legal.

3) (define (f x y) (x 'a y 'b))
In this case f and x are functions, and y is a value (that could potentially be a function!). The function x will be called, with the values 'a, y and 'b being passed into it. At this point y could either be a value, or a function and provided that the function x is expected the actual item in, this code is legal.

No comments:

Post a Comment