]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/ex4_25.rkt
Better explanation of the unless procesure if it is not defined
[sicp.git] / src / sicp / ex4_25.rkt
index 20c41369371e0f919e25195acd3b50c103ec913e..7a01051cf11fd93ea5adb334e42efe4f2193ffad 100644 (file)
@@ -2,9 +2,21 @@
 
 #|
 
-The call (factorial 5) will never call 'unless' and will be recursively call 'factorial' for ever.
+The call (factorial 5) will never call 'unless' and will be recursively call 'factorial' for ever. 'n' goes
+negative and the factorial won't notice it, as it will never get to evaluate the function body of `unless'.
 
 It will work in a normal order language because none of the arguments of 'unless' are evaluated until
 they are used inside.
 
+#lang lazy
+(define (unless predicate if-exp else-exp)
+  (if (not predicate)
+      if-exp
+      else-exp))
+
+(define (factorial n)
+  (unless (= n 1)
+          (* n (factorial (- n 1)))
+          1))
+
 |#
\ No newline at end of file