正在加载图片...
Procedures are values Too (define twice(lambda(f x)(f(f x)) (twice square 2)=? sept.8.2003 16.41016.413 Intro to Scheme A less trivial procedure: factorial Compute n factorial, defined as n! n(n-1)(n-2)(n-3).1 (n-1)(n-2).]=n*(n-1)!ifn>1 (define fact 主 (*n(f ests numerical equality (=44)==>#t true false) 主f(= Sept. 8, 2003 predicate 16.410/1604194qtlbe Schemalternative/ Procedures are Values Too • (define twice (lambda (f x) (f (f x)))) • (twice square 2) = ? Sept. 8, 2003 16.410 16.413 Intro. to Scheme 11 / A less trivial procedure: factorial • (if (= 4 4) 2 3) ==> 2 (if (= 4 5) 2 3) ==> 3 (= 4 4) ==> #t ) (= 4 5) ==> #f (define fact (lambda (n) (if (= n 1) 1 ( ( ) ( Compute n factorial, defined as n! = n(n-1)(n-2)(n-3)...1 •if special form •predicate = (false) n-1 12 )! Sept. 8, 2003 predicate consequent 16.410 16.413 Intro. to Scheme alternative tests numerical equality (true (* n (fact (- n 1)))))) •Notice that n! = n * [ n-1) n-2 ...] = n * if n > 1 6
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有