SICP 1.2.2

Posted on 2015-02-12 19:25:25 +0900 in SICP Lisp

1.11

Recursive versoin:

#lang planet neil/sicp
(define (f-triple n)
  (cond ((< n 3) n)
        (else
          (+ (f-triple (- n 1))
           (f-triple (- n 2))
           (f-triple (- n 3))))))

Iterate version:

(define (f-triple-iter n)
  (if (< n 3)
    n
    (f-iter 2 1 0 (- n 2))))
(define (f-iter a b c count)
  (if (= count 0)
    a
    (f-iter (+ a (* 2 b) (* 3 c))
            a
            b
            (- count 1))))

1.12

(define (triangle row col)
  (cond ((= col 1) 1)
        ((> col row) 0)
        (else
            (+ (triangle (- row 1) (- col 1))
               (triangle (- row 1) col)))))
----------------------------------- 本文内容遵从CC版权协议转载请注明出自kamelzcs -----------------------------------
«  | SICP 1.2.1 »

Hide Comments

comments powered by Disqus