learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-11.scm (896B)


      1 (define-library (sicp solutions chapter-1 exercise-11)
      2   (import (scheme base))
      3   (export f-recursive
      4           f-iterative)
      5 
      6   #!
      7 
      8   *Exercise 1.11:* A function f is defined by the rule that f(n) = n
      9   if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n>= 3.
     10   Write a procedure that computes f by means of a recursive process.
     11   Write a procedure that computes f by means of an iterative
     12   process.
     13 
     14   !#
     15   (begin
     16     (define (f-recursive n)
     17       (cond
     18        ((< n 3) n)
     19        (else (+ (f-recursive (- n 1))
     20                 (* 2 (f-recursive (- n 2)))
     21                 (* 3 (f-recursive (- n 3)))))))
     22 
     23     (define (f-iterative n)
     24       (define (f i n a b c)
     25         (cond
     26          ((= i n) a)
     27          (else (f (+ i 1)
     28                   n
     29                   b
     30                   c
     31                   (+ c
     32                      (* 2 b)
     33                      (* 3 a))))))
     34       (f 0 n 0 1 2))))