[Haskell-beginners] Padding List with Zeros

Greg greglists at me.com
Wed Sep 15 04:43:29 EDT 2010


I think you also need to handle the case where the second parameter to g is empty (where list_A has non-matching elements after list_B is exhausted).

g (a:as) [] = 0 :  g as []

or something similar in between the two existing definitions?


On Sep 15, 2010, at 1:34 AM, jean verdier wrote:

> 
> 
> list_A = [0,10,20,30,40,50]
> list_B = [0,10,50] 
> list_C = [2,1,-5]
> 
> f a b c = g a (zip b c)
> 
> g []     _              = []
> g (a:as) xs@((b,c):xs')
> | a > b  = error "b is not a subset of a"
> | a == b = c : g as xs'
> | a < b  = 0 : g as xs
> 
> main = do
> print (f list_A list_B list_C)
> 
> 
> 
> On Wed, 2010-09-15 at 09:28 +0200, 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



More information about the Beginners mailing list