I am learning about the guard
function from the book 'Learn You a Haskell for Great Good!' by Miran Lipovaca.
For the following example:
ghci> [1..50] >>= (\x -> guard('7' `elem` show x) >> return x)
[7, 17, 27, 37, 47]
I know that guard
takes a Boolean value, and if the value is True
, guard takes ()
and puts it in a minimal default context and succeeds.
If the value is False
, then guard
makes a failed monadic value.
However, I don't understand how guard works in the above example to create the resulting list [7, 17, 27, 37, 47]
. What is passed as x
in the lambda function, is it 1? Further, if ('7' `elem` show x)
evaluates to False
, then wouldn't the empty list be returned? How exactly does the final result list come to be?