haskell-cafe@haskell.org
[Top] [All Lists]

Re: [Haskell-cafe] Haskell and "memoization"

Subject: Re: [Haskell-cafe] Haskell and "memoization"
From: Ozgur Akgun
Date: Thu, 17 Dec 2009 10:28:42 +0000
Sorry for the last mail, I now tried it and it returns the next value every time I call it.

I was using an unsafeperformIO trick somewhere, and that fas the one resulting in the previously described behaviour.

You can just ignore the previous mail.

2009/12/17 Ozgur Akgun <ozgurakgun@xxxxxxxxx>
Maybe not related, but does the following prove next is called once and only once.


import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

next =
ÂÂÂ do
ÂÂÂ ÂÂÂ nextcache <- BS.readFile "next.cache"
ÂÂÂ ÂÂÂ let nextint = readInt (BSC.unpack nextcache)
ÂÂÂ ÂÂÂ BS.writeFile "next.cache" (BSC.pack (show (nextint+1)))
ÂÂÂ ÂÂÂ return nextint

readInt :: String -> Int
readInt = read


I put a single character, 1 in the file "next.cache" when I run this through ghci, and call next several times, I always get a 1. Whereas in the file there is a 2.
I see that next is a trial of creating a function which returns different things everytime its called, but it's in the IO monad, so that should be doable.

When I re-run ghci, now it starts to give 2 everytime I call it. Does that mean, it doesn't bother to re-read the file while we are in the same process.


Hope it relates to the OP's question in some way :)

Best,

2009/12/16 Daniel Fischer <daniel.is.fischer@xxxxxx>

Am Mittwoch 16 Dezember 2009 15:49:54 schrieb michael rice:
> Thanks all,
>
> OK, so this definition of fib
>
> fib 0 = 1
> fib 1 = 1
> fib n = fib (n-1) + fib (n-2)
>
> would involve a lot of recomputation for some large n,

Where "large" can start as low as 20; 60 would be out of reach.

> which memoization would eliminate?

Right.
>
> Michael

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@xxxxxxxxxxx
http://www.haskell.org/mailman/listinfo/haskell-cafe



--
Ozgur Akgun



--
Ozgur Akgun
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@xxxxxxxxxxx
http://www.haskell.org/mailman/listinfo/haskell-cafe
<Prev in Thread] Current Thread [Next in Thread>