Skip to content

Instantly share code, notes, and snippets.

@aoeuidht
Created October 29, 2012 11:14
Show Gist options
  • Save aoeuidht/3973003 to your computer and use it in GitHub Desktop.
Save aoeuidht/3973003 to your computer and use it in GitHub Desktop.
1.11 of sicp
;A function f is defined by the rule that f(n) = n if n<3 and
; f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3.
; Write a procedure that computes f by means of a recursive process.
; Write a procedure that computes f by means of an iterative process.
(defun func_f (n)
(if (< n 4) n
(+ (func_f (- n 1))
(* 2 (func_f (- n 2)))
(* 3 (func_f (- n 3))))))
(print (func_f 6))
(defun iter_f (n)
(if (< n 4) n
(iter_f_iter 1 2 3 n)))
(defun iter_f_iter (n1 n2 n3 n)
(if (< n 4)
n3
(iter_f_iter n2
n3
(+ n3 (* 2 n2) (* 3 n1))
(- n 1))
))
(print (iter_f 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment