[Haskell] Memoization in Haskell

Glynn Clements glynn at gclements.plus.com
Sun Apr 3 22:59:41 EDT 2005


Bright Sun wrote:

> I can not understand memoization in Haskell.  I can
> not find an example except the fib.
> 
> For example, I want drop a list 4 times like, 
> 
> >drop 4 (drop 3 (drop 2 (drop 1
> [1,2,3,4,5,6,7,8,9,10,11,12])))
> >[11,12]
> 
> can I implement a memoization funcation like
> 
> >droploop [1,2,3,4] [1,2,3,4,5,6,7,8,9,10,11,12]
> to get same result
> >[11,12]

That isn't memoization.

Memoization is where you record a set of prior argument/result pairs
(i.e. a lookup table) to avoid having to perform the exact same
calculation repeatedly.

You can implement droploop as a fold, e.g.:

	droploop ns xs = foldr drop xs ns

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the Haskell mailing list