Created
July 27, 2016 08:17
-
-
Save SnowOnion/f3eb1fe5d91cd0f794201d021a787eab to your computer and use it in GitHub Desktop.
Calculate Fibonacci number, in Haskell, with memoization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- via https://wiki.haskell.org/Memoization | |
-- P.S. I just realized memoization != memorization ! | |
-- https://en.wikipedia.org/wiki/Memoization && https://en.wikipedia.org/wiki/Memorization | |
memoized_fib :: Int -> Integer | |
memoized_fib = (map fib [0 ..] !!) | |
where fib 0 = 0 | |
fib 1 = 1 | |
fib n = memoized_fib (n-2) + memoized_fib (n-1) | |
-- It's for some online math problem. The organizer does not recommend discussing solutions outside the forum. | |
main=print $ sum.(filter even) $ takeWhile (<=4000000) $ map memoized_fib [0..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment