Let's say we want to test whether the list [1,2,3,4,5]
contains 2
.
We could do this by turning the list of integers [1,2,3,4,5]
into a list of Booleans [False, True, False, False, False]
indicating,
for each element in the original list, whether it is equal to 2
.
位> x = map (== 2) [1..5]
And then we can loop over it with foldr
, starting with a base value of
False
and taking the Boolean or with each value in x
. So if any
of the values in x
is True
, then the result will end up True
,
telling us that the element is in the list.
位> foldr (||) False x
True
And we see that it is.
Don't worry too much about the details of foldr
; what's next is
the interesting part. We can use the :sprint
command in the REPL
to inspect the value of x
without performing any further evaluation:
位> :sprint x
x = False : True : _
It shows us that x
consists of:
False
(because1 /= 2
), thenTrue
(because2 == 2
), and then- ... an unspecified remainder of the list.
The rest of x
never got evaluated.
In other words, the loop terminated early.
This is because the function we used with foldr
we used to combine the
values was ||
. Once the value accumulated in the loop became True
,
it evaluated to True || 馃摝
, where 馃摝
is the result of folding over
the rest of the list. Since True || 馃摝
is always True
no matter what
馃摝
is, evaluation can just stop there and output True
.
If we print x
to the REPL, we force evaluation of the rest of it.
And we see that it is in fact what we expected:
位> x
[False,True,False,False,False]