annotate 7-Haskell/fibonacci.hs @ 87:2b5341fc4555

Add Haskell Day 1 code and notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Jun 2019 17:31:22 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
87
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 module Fibonacci where
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 -- Classic imperative Fibonacci
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 -- XXX: This seems to give odd answers! "fib_simple 0" gives 1 when it should be 0 (0-based indexing) or an error (1-based indexing)!
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 fib_simple :: Integer -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 fib_simple 0 = 1
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 fib_simple 1 = 1
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 fib_simple x = fib_simple (x - 1) + fib_simple (x - 2)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 -- Fibonacci with tuples and effective caching
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 -- This works by using the parameter as the number of steps and iterating those in the tuple
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 -- Note: We're zero-indexing, so "fib 0" is the first Fibonacci number
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 fib :: Integer -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 fib x = fibResult (fibTuple (0, 1, x))
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 fibResult :: (Integer, Integer, Integer) -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 fibResult (x, y, z) = x
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 fibTuple :: (Integer, Integer, Integer) -> (Integer, Integer, Integer)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 fibTuple (x, y, 0) = (x, y, 0)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 fibTuple (x, y, index) = fibTuple (y, x + y, index -1)