I’d be really grateful if you explain this question for me. I’ve been pondering on it for quite some time but couldn’t make it out. I’m a self-learner and this is an exercise from MIT’ s icampustutor site.
Indicate the type of each of the following procedures.Use the symbols “->” to denote “maps to”, for example, the procedure square has type “number->number”. Use the following terms to describe primitive types of data: number, boolean, string.
1- (define (test bar n) (if (bar n) n (test bar (+ n 2))))
2- (define (test foo bar n) (if (bar n) #t (test foo bar (+ n (foo n)))))
3- (define (test foo bar n) (if (bar n) (+ 1 (foo n)) (test foo bar (+ n 3))))
4- (lambda (a b) (+ a (if b 1 0)))
5- (lambda (x) (lambda (y) (+ x y)))
6- (lambda (x y comp) (if (comp x y) (+ x 1) (+ y 1)))
Thanks for any help.
Your first three expressions bind lambdas to the symbol “test”. The problem is asking you to infer the types of foo and bar from how the function is used and how the result of that function is used. This is what I get out of reading that code:
(n->b n) -> n
That is to say, test takes two parameters, the first being a lambda which takes a number and returns a boolean, and the second being a number, and the result of the function is a number
(n->n n->b n) -> b
(n->n n->b n) -> n
Similarly for 4,5,6, except of course the result of the expression is a lambda itself, and isn’t being bound to anything.
(n b) -> n
n -> (n->n)
That is to say, the result of this lamba is another lambda which takes a number and returns a number.
(n n (n n)->b) -> n
Taking two numbers and a lambda which takes two numbers and returns a boolean