正在加载图片...
Sum list, iterative version 入 Now construct an iterative version (define(sum D) (let loop(( D)(answer o) (if (null? D) answer (loop (cdr D)(+ answer(car sept.17,2003 16.410/16. More Scheme How about list product? Abstract from list sum (define(factorial n) fold .. first-n fold operator initial-value list)L-) (fod+0(1234)=10 (define(factorial n) (define(fold op initial D) (fold *1(first-n n) (if (null? D) initial (op(car I)(fold op initial (cdr D)) (define(list-product D)(fold*1 D) sept.17,2003 16.410/16. More SchemeSept. 17, 2003 5 Sum list, iterative version • (define (sum l) (let loop ((l l) (answer 0)) (if (null? l) answer Sept. 17, 2003 6 How about list product? • • (fold operator initial-value list) (fold + 0 ‘(1 2 3 4)) = 10 (define (fold op initial l) (if (null? l) initial (define (list-product l) (fold * 1 l)) (define (factorial n) …) (define (factorial n) Now construct an iterative version (loop (cdr l) (+ answer (car l)))))) Abstract from list sum. (op (car l) (fold op initial (cdr l))))) … fold … first-n (fold * 1 (first-n n))) 16.410/16.413 More Scheme 16.410/16.413 More Scheme
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有