In Scheme, I modified the basic \'if\' command as:
(define (modified-if predicate then-clause else-clause)
(if predicate
then-clause
else-clause))
Scheme has eager evaluation. This means that, unless you're using a special form (like if
) or a macro (like cond
or case
) that delegates to such a special form, all subexpressions are evaluated first.
That means for your expression
(modified-if (= n 0)
1
(* n (factorial (- n 1))))
the (* n (factorial (- n 1)))
is evaluated first, before modified-if
is run. (It may be run before or after (= n 0)
, but it doesn't matter either way, the recursive call still happens regardless.) And since this is a recursive call, that means that your program will infinitely recurse, and you will eventually run out of stack.
Here's a simple example: consider this:
(if #t
(display "Yay!")
(error "Oh noes!"))
Because if
is a special form, and it only evaluates the necessary branch, in this case it will only evaluate (display "Yay!")
and not evaluate (error "Oh noes!")
. But if you switch to using your modified-if
, both expressions will be evaluated, and your program will raise an error.