Commit 26d2e639 authored by panne's avatar panne
Browse files

[project @ 2000-06-29 19:47:50 by panne]

There is not much point in memoising Integers, so we should better use
unary "numbers" (i.e. lists of units) instead
parent b8d23d80
module Main
where
module Main where
import Memo
import System(getArgs)
import Memo ( memo )
import List ( genericLength, genericReplicate )
import System ( getArgs )
main :: IO ()
main = do (arg:_) <- getArgs
mapM_ printTriple [ (i,fib i,mfib i) | i <- [10..read arg] ]
where printTriple (i,fi,mfi) = do print i
......@@ -11,13 +12,17 @@ main = do (arg:_) <- getArgs
print mfi
putStrLn ""
-- There is not much point in memoising Integers, so we use unary "numbers" instead
mfib :: Integer -> Integer
mfib = memo ufib
mfib = genericLength . mfib' . flip genericReplicate ()
ufib :: Integer -> Integer
ufib 0 = 1
ufib 1 = 1
ufib n = mfib (n-1) + mfib (n-2)
mfib' :: [()] -> [()]
mfib' = memo ufib
ufib :: [()] -> [()]
ufib [] = [()]
ufib [()] = [()]
ufib (():n1@(():n2)) = mfib' n1 ++ mfib' n2
fib :: Integer -> Integer
fib 0 = 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment