Lab 8

For these programs, you may use only "lambda" to define your functions, and the solution to each problem must be a single scheme term, defined using a ’define’.

So, for instance: define a function p1 that takes a function and applies it to 9:

;; solution:

  (define p1 (lambda (f) (f 9)))

;; test case:

  (test (p1 (lambda (x) (+ x 3))) 12)

WARMUP:

- Define a function called one that accepts a function and an argument and applies the function to the argument.

CONTINUING ON:

  1. Define a function called two that accepts a function and an argument and applies the function to the result of applying the function to the argument.

  2. Define a function called zero that accepts a function and an argument and returns the argument.

  3. Define a function called add1 that accepts a function like zero or one and returns a function that does the function "one more time". So, for instance, calling add1 with two should produce the function that applies its first argument to its second argument three times.

  4. Define a function called ’add’ that accepts two functions like zero and one and returns a function that applies its first argument to its second argument a number of times that corresponds to the sum of the two ’numbers’ it was given. So, if called with the ’three’ function and the two function, it should produce a function that applies its first argument to its second argument five times.

  5. Define a function called true that accepts two arguments and returns the first one.

  6. Define a function called false that accepts two arguments and returns the second one.

  7. Define a function called ’if’ that accepts three arguments. If the first one turns out to be the function true (as above), it returns the result of the second argument. If the first one turns out to be the function false (as above), it returns the result of the third argument. This function should not use any of racket’s conditional operators (if, cond, etc.) Show me this function for lab credit.

VERY HARD:

- Define a function called ’sub1’ that ... well, subtracts one from a ’number’. Yes, it can be done. Yes, it’s quite tricky.