<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
<font style="" face="Courier New">I can calculate non-nested list comprehensions without a problem, but am unable to calculate nested comprehensions involving, for example, the generation of a list of pairs where the first and separate elements are drawn from two separate lists, as in:<br><br></font><font style="" face="Courier New"> [(a, b) | a <- [1..3], b <- [1..2]]</font><font style="" face="Courier New"><br>
</font><font style="" face="Courier New"><br>How does one calculate the expansion of this list? The two rules for expanding list comprehensions are:</font><br><font style="" face="Courier New"><br>1. Generator rule: [e | x <- xs, Q] = concat (map f xs)</font><font style="" face="Courier New"><br></font><font style="" face="Courier New"> where</font><font style="" face="Courier New"><br></font><font style="" face="Courier New"> f x = [e | Q]</font><font style="" face="Courier New"><br></font><font style="" face="Courier New"><br></font><font style="" face="Courier New">2. Guard rule: [e | p, Q] = if p then [e | Q] else []</font><font style="" face="Courier New"><br></font><font style="" face="Courier New"><br></font><font style="" face="Courier New"><br></font><font style="" face="Courier New">There is a third rule that I've seen on the Internet, not in an authoritative text:<br>
</font><font style="" face="Courier New"><br>
[e | Q1 , Q2] = concat [ [e | Q 2] | Q1 ]<br>
<br>
I don't understand what this third rule means, or whether it's relevant.<br>
<br></font><font style="" face="Courier New">Concat and map are defined as:<br><br>concat :: [[a]] -> [a]<br>concat [] = []<br>concat (xs:xss) = xs ++ concat xss<br><br>map :: (a -> b) -> [a] -> [b]<br>map f [] = []<br>map f (x:xs) = f x : (map f xs)<br></font><font style="" face="Courier New"><br>Any help is appreciated.<br></font><font style="" face="Courier New">
</font><font style="" face="Courier New"><br>
</font><br><br /><hr />Windows Live™: Keep your life in sync. <a href='http://windowslive.com/explore?ocid=TXT_TAGLM_WL_allup_1b_explore_032009' target='_new'>Check it out.</a></body>
</html>