-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises_1_2_2.scm
35 lines (28 loc) · 963 Bytes
/
exercises_1_2_2.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;;; Exercise 1.11 Define the folling function recursively and iteratively.
;;; f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3
;;; Recursive
(define (f n)
(cond ((< n 3) n)
(else (+ (f (- n 1))
(* (f (- n 2)) 2)
(* (f (- n 3)) 3)))))
(f 5)
;;; Iterative
(define (f n)
(f-iter n 3 2 1 0))
(define (f-iter n count fn-one fn-two fn-three)
(cond ((< n 3) n)
((= n count) (+ fn-one (* 2 fn-two) (* 3 fn-three)))
(else (f-iter n
(+ count 1)
(+ fn-one (* 2 fn-two) (* 3 fn-three))
fn-one
fn-two))))
(f 5)
;;; Exercise 1.12 Define function to compute element of Pascal's triangle
(define (pascal-element depth position)
(if (or (= depth 1) (= position 1) (= position depth))
1
(+ (pascal-element (- depth 1) (- position 1))
(pascal-element (- depth 1) position))))
(pascal-element 5 2)