I've been working through Graham Hutton's Programming in Haskell. It states that
the function
mapapplies a function to all elements of a list
OK, makes sense. Certainly matches what I know about map from other languages.
One of the exercises is to implement all :: (a -> Bool) -> [a] -> Bool.
There are times where a naive implementation may loop infinitely if not done lazily. For example all (<10) [1..]. So my first implementation was:
all p [] = True
all p (x:xs) | not (p x) = False
| otherwise = all' p xs
But then I thought about trying with function composition of map and and to see what a non terminating programme in Haskell would do:
all' :: (a -> Bool) -> [a] -> Bool
all' p = and . map p
To my surprise, all' (<10) [1..] quickly returned False. I actually expected map to attempt to create an infinite list before and was applied - something like and [p x | x <- xs, p x].
This terminating behaviour would imply that map is in fact creating something similar to a stream which and is processing. Is map in fact lazy (and hence the description wrong) or have I misunderstood something else in my second implementation?