[Haskell-beginners] Padding List with Zeros
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:
> d  _ _ = 
> d a  c = d a  c
> d a b  = d a b 
> 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.
> 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).
>> On 09/15/2010 01:55 AM, Antoine Latter wrote:
>>> Are these truly lists, or would you be better suited using Sets, Maps or
>>> 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
>>> 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
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners