<br><br>
<div class="gmail_quote">2009/3/27 Michael Mossey <span dir="ltr"><<a href="mailto:mpm@alumni.caltech.edu">mpm@alumni.caltech.edu</a>></span><br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Is there a way to pattern match to a list argument and get a list? For example, I have this:<br><br>-- A LayoutItem has a center position (point), and the four ints are<br>
-- "left width", "rigth width", "top height", and "bottom height"<br>data LayoutItem = LayoutItem Point Int Int Int Int<br> deriving Show<br><br>-- This computes the left width of a group of composited LayoutItem.<br>
compositeLeftWidth :: [LayoutItem] -> Int<br>compositeLeftWidth items = let<br> itemsPosX = [ x | LayoutItem (Point (x,_)) _ _ _ _ <- items ]<br> itemsLW = [ lw | LayoutItem _ lw _ _ _ <- items ]<br> z = zipWith (\x y -> x-y+1) itemsPosX itemsLW<br>
in (minimum z) - 1<br><br>What I'm wondering is if I could somehow do something like<br><br>compositeLeftWidth [ LayoutItem Point( x, _ ) lw _ _ _ ] = ...<br><br>and that x and lw would each be the lists I'm calling itemsPosX and itemsLW above.<br>
<br>Thanks,<br>Mike<br><br></blockquote>
<div>That would be neat :-). But IIRC there is no syntax for that.</div>
<div>However, if you are interested in other suggestions (by a very newbie, so they could be off-mark):</div>
<div> </div>
<div>1. if you use a definition like this </div>
<div> </div>
<div> data LayoutItem = LayoutItem { x :: Point; leftw, rightw, toph, bottomh :: Int }<br> deriving Show<br></div>
<div> then you could do, using the data accessor functions implicitely defined by the above code:</div>
<div> </div>
<div> xlist = map x items</div>
<div> lwlist = map leftw items</div>
<div> </div>
<div>which is basically the same of your list comprehensions, but takes less characters :-)</div>
<div> </div>
<div>2. If I read correctly your code, you are processing a single element of 'items' per time. If so, instead</div>
<div> of building lists of attributes and the zipWith them to get your result, you could use this approach:</div>
<div> </div>
<div>a. define a function tha process a single item of type Layout, like :</div>
<div> </div>
<div> compLW LayoutItem x lw rw th bh = x - lw + 1 </div>
<div> -- not sure it has the same result, but you get the idea</div>
<div> </div>
<div>b. use map (or list comprehension) to build z from items:</div>
<div> </div>
<div> compositeLeftWidth items = ( minimum z) -1 </div>
<div> where z = map compLW items </div>
<div> </div>
<div>This way you avoid to scan multiple times the same list.</div>
<div> </div>
<div>Ciao</div>
<div>-------</div>
<div>FB</div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div></div>