FWIW here is another solution, using only lists. (Whereas I agree with others who suggested using <i>proper</i> data structures rather than 'lists for everything')<br><br><div><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">-- walks over both lists, always consumes an element from </span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">-- the first list, </span><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">consumes an element from the second list</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">-- only if values are equal.</span></div><div><div><font class="Apple-style-span" face="'courier new', monospace">ix :: Ord a => [a] -> [a] -> [b -> Maybe b]</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">ix (a:as) (b:bs) | a == b = Just : ix as bs</font></div><div><font class="Apple-style-span" face="'courier new', monospace">ix (a:as) (b:bs) | a < b = const Nothing : ix as (b:bs)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">ix _ _ = []</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">-- consume a value from the second list, only if the current</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">-- function coming from the </font><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">first list evaluates to a Just</span></div>
<div><font class="Apple-style-span" face="'courier new', monospace">proj :: [a -> Maybe b] -> [a] -> [Maybe b]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">proj [] _ = []</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">proj (f:fs) (x:xs) =</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> let fx = f x</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> nextxs Nothing = x : xs</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> nextxs _ = xs</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> in fx : proj fs (nextxs fx)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><div><font class="Apple-style-span" face="'courier new', monospace">*Main> proj (ix inp_a inp_b) inp_c</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">[Just 2,Just 1,Nothing,Nothing,Nothing,Just (-5)]</font></div></div><div><br></div><div>And of course, if you really want to substitute 0's for Nothings, you can do it using something like:</div>
<div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">to0s :: Num a => [Maybe a] -> [a]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">to0s = map (fromMaybe 0)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><div><font class="Apple-style-span" face="'courier new', monospace">*Main> to0s $ proj (ix inp_a inp_b) inp_c</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">[2,1,0,0,0,-5]</font></div></div><div><br></div><div>Hope this helps,</div><div>Ozgur</div><div><br><div class="gmail_quote">On 15 September 2010 00:35, Lorenzo Isella <span dir="ltr"><<a href="mailto:lorenzo.isella@gmail.com">lorenzo.isella@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Dear All,<br>
I still have to find my way with immutable lists and list comprehension.<br>
Consider the following lists<br>
<br>
A=[0,10,20,30,40,50]<br>
B=[0,10,50] (i.e. B is a subset of list A; list A is already ordered in increasing order and so is B).<br>
C=[2,1,-5] i.e. there is a corresponding element in C for every element in B.<br>
<br>
Now, I would like to define a new list D having length equal to the length of A. The elements of D in the position of the elements of A in common with B are equal to the corresponding entries in C, whereas the other ones are zero i.e.<br>
D=[2,1,0,0,0,-5]. How can I achieve that? The first thought that comes to my mind is to define a list of zeros which I would modify according to my needs, but that is not allowed...<br>
Many thanks<br>
<br>
Lorenzo<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br><br>
</div></div></div>