

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


On the actual midterm, you will have plenty of space to put your answers.
iii. tail-recursive. Solution: a. The first program generates a stack overflow, while the second program runs indefinitely. b. Both programs are recursive, and in fact forward recursive, but loop2 is tail- recursive while loop1 is not. c. Because loop1 is not tail-recursive, each new recursive call must push a new activation record onto the stack, hence the stack overflow, but since loop2 is tail- recursive, each new activation record may overwrite the previous call, and thus the stack does not grow. It should also be observed that loop2 has a more general type ( :unit -> โa ) than that of loop1:unit -> unit , and hence may be used in places where other return types beside unit are required. (Of course, it had never actually be applied.)