If I call the following Haskell code
find_first_occurrence :: (Eq a) => a -> [a] -> Int
find_first_occurrence elem list = (snd . head) [x | x <- zip list [0..], fst x == elem]
with the arguments
'X' "abcdXkjdkljklfjdlfksjdljjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
how much of the zipped list [('a',0), ('b',1), ]
is going to be built?
UPDATE:
I tried to run
find_first_occurrence 10 [1..]
and returns 9
almost instantly, so I guess it does use lazy evaluation at least for simple cases? The answer is also computed "instantly" when I run
let f n = 100 - n
find_first_occurrence 10 (map f [1..])
seq
. – Daniel Wagnercase zip [1..] [2..] of (a,b):_ -> True
evaluates toTrue
, because we only need the first element of the list to match with(a,b):_
. – Riccardo T.foo
against the patternx
, or_:_
, or(_:_):_
, the runtime won't evaluatefoo
any farther than it needs to to decide which pattern matches. – Daniel Wagner