Home > database >  Does calling map twice on the same list forces iterate through the list twice?
Does calling map twice on the same list forces iterate through the list twice?

Time:12-16

I am completely newbie in Haskell and reading tutorial I have found something like that

Thanks to Haskell's laziness, even if you map something over a list several times and filter it several times, it will only pass over the list once.

That's my simple stupid example: x = map foo (map foo [1,2]) where foo p = p * 2

Now, to my mind came two questions.

  1. Why is it thanks to laziness? I know that laziness means that it does not evaluate expression till it has to but... How does it force iterating list once instead of declared number of maps/filters?
  2. How can I verify that it iterates only once?
  3. Bonus question - I know that side effections are
  • Related