Let's say we want a collection of values that are computed on demand (the computation has some side effects), and not recomputed again when you access them again.
The following two naive approaches will not work:
Prelude> let f x = print x >> return x
Prelude> let a = map f "abc"
Prelude> :t a
a :: [IO Char]
Prelude> head a
'a'
'a'
Prelude> head a
'a'
'a'
Prelude> let b = mapM f "abc"
Prelude> :t b
b :: IO [Char]
Prelude> fmap head b
'a'
'b'
'c'
'a'
Prelude> fmap head b
'a'
'b'
'c'
'a'
How do I do it correctly? As of now, I don't even understand what type the collection should have.