[Haskell-beginners] Cartesian Product in Standard Haskell Libraries

Chaddaï Fouché chaddai.fouche at gmail.com
Mon Dec 24 10:42:16 CET 2012


On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger <jays at panix.com> wrote:
>
>   > sequence []
>   []
>   it :: [()]
>
> This looks to me to be a violation of the rule that the Cartesian
> product of an empty list of lists is a list with one element in
> it.  It looks to be a violation because "[]" looks like a name
> for an empty list.  But we also have
>
>   > length (sequence [])
>   1
>   it :: Int
>
> which almost reassures me.
>

Well the type of the first response is a dead give-away : the result
is of type [()] but for a cartesian n-product, you would like [[a]]
(with a maybe instantiated to a concrete type) ...
What's happening here is that sequence is not "the cartesian
n-product" in general, it is only that in the list monad but in
"sequence []" there's nothing to indicate that we're in the list
monad, so GHC default to the IO monad and unit () so sequence has the
type "[IO ()] -> IO [()]" and there's no IO action in the list
parameter, so there's nothing in the result list.

Try :
> sequence [] :: [[Int]]
and you should be reassured.

--
Jedaï



More information about the Beginners mailing list