In racket, Given the function definition:
(define (guess a_list)
(if (null? a_list)
0
(+ 1 (guess (cdr a_list)))
))
(a) write the derivation of the following function call: (guess ‘(3 1 2 5)) =>
(b) explain what this function is doing when given a parameter a_list (NOT the steps of execution, but the result).
Solution:
Given function definition:
(define (guess a_list)
(if (null? a_list)
0
(+ 1 (guess (cdr a_list)))
))
a.Ans:
(guess ‘(3 1 2 5))
= 1 + (guess (1 2 5))
= 1 + 1 + (guess (2 5))
= 1 + 1 + 1 + (guess (5))
= 1 + 1 + 1 + 1 + (guess ())
= 1 + 1 + 1 + 1 + 0
= 4
b.Ans:
(guess ‘(3 1 2 5))
calls the guess function here the list is [3 1 2 5]
if the list is empty then return 0
else add 1 to the recursive guess function until the list becomes 0
Finally the result will be 4 as there are 4 elements in the list.
Get Answers For Free
Most questions answered within 1 hours.