Lab 6
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)
1 Warmup
Define a function called one that accepts a function and an argument and applies the function to the argument.
2 Continuing on:
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.
Define a function called zero that accepts a function and an argument and returns the argument.
Let’s use the term “number-like functions” for functions like zero, one, and two. Define a function called add1 that accepts a number-like function and returns a new number-like 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. This function should not use racket’s numbers at all, just the number-like functions described above.
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.
Define a function called tru that accepts two arguments and returns the first one.
Define a function called fals that accepts two arguments and returns the second one.
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.
3 Very Hard
Define a function called ’sub1’ that ... well, subtracts one from a ’number’. Yes, it can be done. Yes, it’s quite tricky.
Can you define a recursive function?