I am asked to interpret the output of a function with specific inputs but I don’t understand how the function works. It is suppose to be a new version of if but to me it looks like it does nothing at all.
(define (if-2 a b c)
(cond (a b)
(else c)))
To me this just looks like it will always print b but I am not sure.
It seems like you are unfamiliar with the
condform. It works like this:Below is your code with some variables renamed.
To figure out why it is always returning
arg1for your tests, recall that Scheme sees everything as true except the explicit false symbol (usually#f) and the empty list'().So when you call
(if-2 > 2 3)the cond form evaluates to this:Then since
condreturns the first thing it finds to be associated with a true value you get 2 back.To make
if-2work as expected you need to call it differently, e.g.(if-2 (> 3 2) 'yep! 'nope!)will return'yep!since 3 is greater than 2.