摘要: f(n) = n; n=3递归1 ;; Scheme code2 (define (foo1 n)3 (cond4 ((< n 3) n)5 (else (+6 (foo1 (- n 1))7 (* 2 (foo1 (- n 2)))8 (* 3 (foo1 (- n 3)))))))迭代 1 ;; Scheme code 2 (define (foo2 n) 3 (local ((define (f a b c counter) 4 (cond 5 (... 阅读全文
posted @ 2013-07-18 21:24 maxima 阅读(325) 评论(0) 推荐(0) 编辑
摘要: ;;预备函数(define (atom? x) (and (not (pair? x)) (not (null? x))))(define (sub1 x) (- x 1))(define (add1 x) (+ x 1));辅助函数,方便用来显示结果(define-syntax print (syntax-rules () ((_) #f) ((_ e) (begin (display 'e) (display " => ") (display e) (newlin... 阅读全文
posted @ 2013-07-18 21:23 maxima 阅读(363) 评论(0) 推荐(0) 编辑
摘要: 1 ;; Common lisp 2 3 ;; define function 4 5 ;; function as the value 6 7 ;; function as the parameter 8 (apply #'+ '(1 2)) 9 (apply (symbol-function '+) '(1 2))10 (apply #'(lambda (x y) (+ x y)) '(1 2))11 12 (funcall #'+ 1 2 3 4 5)13 14 (mapcar #'(lambda (x) (* x x)) 阅读全文
posted @ 2012-03-13 12:37 maxima 阅读(183) 评论(0) 推荐(0) 编辑
摘要: Ackerman数学函数1 ;; Scheme code2 (define (ackermann x y)3 (cond4 ((= y 0) 0)5 ((= x 0) (* 2 y))6 ((= y 1) 2)7 (else (ackermann (- x 1)8 (ackermann x (- y 1))))))这是个神马函数,增长速度太快(ackermann 4 3)就把我内存算爆掉.前面几个简单序列1 (define (foo1 n) (ackermann 0 n))2 // The value is 2*n3 4 (de... 阅读全文
posted @ 2011-11-15 15:48 maxima 阅读(240) 评论(0) 推荐(0) 编辑
摘要: 应用序和正则序主要是解释顺序上面的区别.应用序首先对参数求值后再代换,而正则序在代换完全后再归约求值.应用序和正则序测试1 ;; Scheme code2 (define (p) (p))3 4 (define (test x y)5 (if (= x 0)6 07 y))8 9 (test 0 (p))应用序首先对(p)不断求值,陷入无限循环,正则序首先完全展开表达式,后求值为0.为什么if是特殊表达式if语法是这个样子的 (if predicate then-clause else-clause)当predicate为真的时候只对then-clause求值,而... 阅读全文
posted @ 2011-11-15 14:06 maxima 阅读(537) 评论(0) 推荐(0) 编辑