3 ms·
main = let fib 30 = 832040 in do putStrLn (show (fib 30)) This is not what I would normally consider 'lazy' or 'memoization'... Not to be too pedantic but th
by flatline 15y ago
main = let fib 30 = 832040 in do
putStrLn (show (fib 30))
This is not what I would normally consider 'lazy' or 'memoization'...
Not to be too pedantic but that 'sieve' is also not really the sieve of Eratosthenes either, as it continually recomputes each slot:
http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
- ricree 15y agoWorse, I think it actually overwrites the fib he just defined with a function that simply pattern matches on 30 and returns 832040. In GHCI, trying that example with fib 29 results in a non exhaustive pattern error.
- ramchip 15y agoExactly; just like let 2+2=5 in 2+2 amusingly returns 5.
- samstokes 15y agoYes, that line doesn't redefine the expression 2+2 as the author suggests; rather, it redefines the operator + as a function which only works on 2 and 2, and returns 5 for those arguments. e.g. Prelude> let 2+2=5 in 2+3 *** Exception: <interactive>:1:5-9: Non-exhaustive patterns in function +
- lelf 15y agoAnd, considering syntactic sugar for operators, fib and (+) examples are quite alike Prelude> let (+) 2 2 = 5; 4 + 2 = 42 in [2 + 2, (+) 4 2, 0 + 3] [5,42,*** Exception: <interactive>:1:5-29: Non-exhaustive patterns in function + Prelude> let fib 0 = 1 in fib 0 1