[Haskell-beginners] Why does this list comprehension return an empty list?
Magnus Therning
magnus at therning.org
Tue Jul 23 11:17:59 CEST 2013
On Tue, Jul 23, 2013 at 10:53 AM, Costello, Roger L. <costello at mitre.org> wrote:
> Hi Folks,
>
> I have a list of singletons:
>
> xs = [("a")]
The type of that, as written, is [String].
> f is a function that, given an argument x, it returns the argument:
>
> f x = x
>
> g is a function that, given an argument x, it returns the empty list:
>
> g x = []
>
> I have a list comprehension that extracts the singletons from xs using f and g, and creates a pair from their output:
>
> [(a,b) | a <- f xs, b <- g xs]
>
> I executed this and the result is the empty list:
>
> []
>
> That is odd. Why is the empty list the result?
If you say it out loud what you are looking for it makes sense:
construct a list tuples containing all combinations of 'a' and 'b'
where 'a' is taken from the list 'f xs' and 'b' from the list 'g xs'.
Now 'g xs' is empty, which means there is no possible value for 'b' to
take, hence there is no way to create even a single such tuple.
/M
--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: magnus at therning.org jabber: magnus at therning.org
twitter: magthe http://therning.org/magnus
More information about the Beginners
mailing list