X-Git-Url: https://git.rkrishnan.org/?p=sicp.git;a=blobdiff_plain;f=src%2Fsicp%2Fex4_25.rkt;fp=src%2Fsicp%2Fex4_25.rkt;h=7a01051cf11fd93ea5adb334e42efe4f2193ffad;hp=20c41369371e0f919e25195acd3b50c103ec913e;hb=d8388032e6e749f7cab7a18d840bc6462826dccc;hpb=f225ca5e374ba315b4fefb9d24174cb942c59036 diff --git a/src/sicp/ex4_25.rkt b/src/sicp/ex4_25.rkt index 20c4136..7a01051 100644 --- a/src/sicp/ex4_25.rkt +++ b/src/sicp/ex4_25.rkt @@ -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