I'd like to pre-calculate some values that are then used when I need to do further lookups. I came up with the following:
import qualified Data.Vector.Unboxed as V
lcgen s =
lc
where
lc 0 b = lengths V.! b
lc a b = lengths V.! b - lengths V.! (a - 1) - 1
lengths = V.fromList $ scanl1 ((+) . (1 +)) $ map length $ words s
The function essentially returns the number of characters used in-between two words. I use it as follows:
let lc = lcgen "some sentence with a lot of words"
lc 0 0 -- == 4
lc 0 1 -- == 13
In this implementation, would the lengths
vector be memoized? Furthermore, how can I know and/or confirm this?