Initial commit
This commit is contained in:
parent
795e4a83d5
commit
1ee6f1ab68
47
SICP/exercise_1_11.janet
Normal file
47
SICP/exercise_1_11.janet
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
(defn f1 [n]
|
||||||
|
(cond
|
||||||
|
(< n 3) n
|
||||||
|
(+
|
||||||
|
(f1 (- n 1))
|
||||||
|
(* 2 (f1 (- n 2)))
|
||||||
|
(* 3 (f1 (- n 3))))))
|
||||||
|
|
||||||
|
(defn f2-s [ a b c ]
|
||||||
|
(+ a (* 2 b) (* 3 c)))
|
||||||
|
|
||||||
|
(defn f2-iter [ a b c i n ]
|
||||||
|
(cond
|
||||||
|
(= i 0) (cond
|
||||||
|
(= n 0 ) a
|
||||||
|
(f2-iter a b (f2-s a b c) (+ i 1) (- n 1)))
|
||||||
|
(= i 1) (cond
|
||||||
|
(= n 0 ) c
|
||||||
|
(f2-iter a (f2-s c a b) c (+ i 1) (- n 1)))
|
||||||
|
(= i 2) (cond
|
||||||
|
(= n 0 ) b
|
||||||
|
(f2-iter (f2-s b c a) b c 0 (- n 1)))))
|
||||||
|
|
||||||
|
(defn f2 [n]
|
||||||
|
(cond
|
||||||
|
(< n 3 ) n
|
||||||
|
(f2-iter 2 1 0 0 (- n 2))))
|
||||||
|
|
||||||
|
(defn f3-iter [a b c n]
|
||||||
|
(def new_c (f2-s a b c))
|
||||||
|
(def new_b (f2-s new_c a b))
|
||||||
|
(def new_a (f2-s new_b new_c a))
|
||||||
|
(cond
|
||||||
|
(= n 1) new_c
|
||||||
|
(= n 2) new_b
|
||||||
|
(= n 3) new_a
|
||||||
|
(f3-iter new_a new_b new_c (- n 3))))
|
||||||
|
|
||||||
|
(defn f3 [n]
|
||||||
|
(cond
|
||||||
|
(< n 3) n
|
||||||
|
(f3-iter 2 1 0 (- n 2))))
|
||||||
|
|
||||||
|
(print (f3 30))
|
||||||
|
(print (f2 30))
|
||||||
|
(print (f1 30))
|
||||||
Loading…
Reference in New Issue
Block a user