annotate 6-Clojure/fibonaci.clj @ 89:7e4afb129bef

Add initial Day 2 notes with functions, partially applied, and currying
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Jun 2019 21:07:27 +0100
parents eccc649d49a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
83
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 ; For a pair of (assumed) consecutive (assumed) Fibonacci numbers and generates the next pair
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 (defn fib-pair [[a b]] ; I *think* this a vector and not just two parameters so that we can iterate (because you can't return two values)
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 [b (+ a b)])
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 (print (fib-pair [3 5]))
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 (print
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 (take 5
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 (map first (iterate fib-pair [1 1]))))
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 (print
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 (nth
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 (map first (iterate fib-pair [1 1]))
eccc649d49a2 Add Day 2 and Day 3 notes and example code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 50)) ;The book says 500th, but even 100th gives java.lang.ArithmeticException: integer overflow!