[Haskell-beginners] Padding List with Zeros

Greg greglists at me.com
Wed Sep 15 04:07:13 EDT 2010


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

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


More information about the Beginners mailing list