[Haskell-beginners] Padding List with Zeros

Greg greglists at me.com
Wed Sep 15 04:16:34 EDT 2010


I'm realizing that this will misbehave if a starts with values less than zero and a zero appears after b is exhausted.  My second definition of d stuffs zeros into b after it runs dry, which might accidentally match with a if there are still zeros in a.  That can obviously be worked around...

I probably should have chosen different parameter names for d as well to make it explicit that they are function arguments and not the same a,b,c as defined earlier.  The argument 'a' is mapped to the defined list 'a' when the function is called inside main...


On Sep 15, 2010, at 1:07 AM, Greg wrote:

> Hey Lorenzo--
> 
> Here's at least a partial solution to the specific case you describe:
> 
> a=[0,10,20,30,40,50]
> b=[0,10,50]
> c=[2,1,-5]
> 
> d [] _ _ = []
> d a [] c = d a [0] c
> d a b [] = d a b [0]
> d a b c | head a == head b = (head c) : d (tail a) (tail b) (tail c)
>         | otherwise = 0 : d (tail a) b c
> 
> main = do
>   print $ d a b c
> 
> I don't know if it will do what you want if the lists aren't perfectly sized as they are in your example (meaning your example has all the lists expire at exactly the same iteration).  In this example it will pad out to zero to the size of a.
> 
> Someone may come up with a better example, I'm still learning as I go too.  I was just looking for an opportunity to give back to the list.
> 
> Cheers--
>   Greg
> 
> 
> 
> 
> On Sep 15, 2010, at 12:28 AM, Lorenzo Isella wrote:
> 
>> Hi Antoine,
>> Unfortunately these are really truly lists and not sets (for instance, the ordering of elements matter and some of them may be repeated).
>> 
>> Lorenzo
>> 
>> On 09/15/2010 01:55 AM, Antoine Latter wrote:
>>> Are these truly lists, or would you be better suited using Sets, Maps or
>>> IntMaps?
>>> 
>>> Then you can use some of the unionWith functions to decide what to
>>> insert, or you can simply wrap the looking functions to return zero on
>>> failure.
>>> 
>> 
>> 
>> 
>>> Antoine
>>> 
>>> On Sep 14, 2010 6:35 PM, "Lorenzo Isella" <lorenzo.isella at gmail.com
>>> <mailto:lorenzo.isella at gmail.com>> wrote:
>>> > Dear All,
>>> > I still have to find my way with immutable lists and list comprehension.
>>> > Consider the following lists
>>> >
>>> > A=[0,10,20,30,40,50]
>>> > 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).
>>> > C=[2,1,-5] i.e. there is a corresponding element in C for every element
>>> > in B.
>>> >
>>> > 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.
>>> > 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...
>>> > Many thanks
>>> >
>>> > Lorenzo
>>> > _______________________________________________
>>> > Beginners mailing list
>>> > Beginners at haskell.org <mailto:Beginners at haskell.org>
>>> > http://www.haskell.org/mailman/listinfo/beginners
>>> 
>> 
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100915/72165817/attachment.html


More information about the Beginners mailing list