[Haskell-beginners] foldr on infinite list to decide prime number
Francesco Ariis
fa-ml at ariis.it
Wed Feb 3 01:55:09 UTC 2016
On Tue, Feb 02, 2016 at 05:52:08PM -0700, derek riemer wrote:
> Doesn't foldr have to "reach" to the far right of the list of infinite
> primes to get the last number since it consumes from the right?
foldl is /left/ associative, foldr /right/ associative.
λ> foldl1 (-) [1..3]
-4
-- which is: ((1-2)-3)
λ> foldr1 (-) [1..3]
2
-- which is (1-(2-3))
Haskell being non strict (i.e. "proceeding from the outside"), it will
immediately short circuit on foldr but have to build the whole list first
on foldl.
More information about the Beginners
mailing list