Paste number 20157: memoize

Index of paste annotations: 1

Paste number 20157: memoize
Pasted by: sjanssen
2 years, 7 months ago
#haskell
Paste contents:
Raw Source | XML | Display As
import Array

f :: Integer -> Integer
f x | x <= 256  = arr ! x
    | otherwise = pureF x
 where
    pureF y = 2^y
    arr     = array (0, 256) [(i, pureF i) | i <- [0..256]]

Annotations for this paste:

Annotation number 1: CAF in where
Pasted by: sjanssen
2 years, 7 months ago
Paste contents:
Raw Source | Display As
import Array
import Debug.Trace

f :: Integer -> Integer
f x | x <= 256  = arr ! x
    | otherwise = pureF x
 where
    pureF y = trace (show y) 2^y
    arr     = array (0, 256) [(i, pureF i) | i <- [0..256]]

main = print $ sum $ map f [1..10]

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.