Skip to content

Commit e554c00

Browse files
committed
enhancing the Math:fibo function to hide the accumulators
1 parent 2f213e8 commit e554c00

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Math.ark

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
# =begin
5454
# (math:fibo 45 0 1)
5555
# =end
56-
(let math:fibo (fun (n p c) {
57-
(if (<= n 0)
58-
0
59-
(if (= n 1)
60-
c
61-
(math:fibo (- n 1) c (+ p c))))}))
56+
(let math:fibo (fun (n) {
57+
(let impl (fun (n p c)
58+
(if (<= n 0) 0
59+
(if (= n 1) c
60+
(impl (- n 1) c (+ p c))))))
61+
(impl n 0 1)}))
6262

6363
# @brief Returns the list of a number's divisors
6464
# @param n the number

tests/math-tests.ark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
(set tests (assert-eq (math:pow 2 2) 4 "math:pow" tests))
2222
(set tests (assert-eq (math:pow 4 0.5) 2 "math:pow" tests))
2323
# small trick with toNumber because we have number's approximation because of the underlying double
24-
(set tests (assert-eq (math:fibo 31 0 1) (toNumber "1346269") "math:fibo" tests))
24+
(set tests (assert-eq (math:fibo 31) (toNumber "1346269") "math:fibo" tests))
2525
# small trick with toNumber because we have number's approximation because of the underlying double
26-
(set tests (assert-eq (math:fibo 32 0 1) (toNumber "2178309") "math:fibo" tests))
26+
(set tests (assert-eq (math:fibo 32) (toNumber "2178309") "math:fibo" tests))
2727
(set tests (assert-eq (math:divs 6) [1 2 3 6] "math:divs" tests))
2828
(set tests (assert-eq (math:divs 2) [1 2] "math:divs" tests))
2929
(set tests (assert-eq (math:divs 931) [1 7 19 49 133 931] "math:divs" tests))

0 commit comments

Comments
 (0)