
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

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"
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

You're right, i didn't compile with -fwarn-incomplete-patterns and it bite me. You may also write g as [] = map (\_ -> 0) as or some combination of take length and repeat. g as [] = take (length as) (repeat 0) I'm sorry to have posted incomplete and uncommented code, I should think twice next time. On Wed, 2010-09-15 at 01:43 -0700, Greg wrote:
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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I'm sorry to have posted incomplete and uncommented code, I should think twice next time.
I can hardly complain after having done the same two posts earlier... =) I botched the same condition, too-- go figure. On Sep 15, 2010, at 1:54 AM, jean verdier wrote:
You're right, i didn't compile with -fwarn-incomplete-patterns and it bite me. You may also write g as [] = map (\_ -> 0) as or some combination of take length and repeat. g as [] = take (length as) (repeat 0)
I'm sorry to have posted incomplete and uncommented code, I should think twice next time.
On Wed, 2010-09-15 at 01:43 -0700, Greg wrote:
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"
mailto:lorenzo.isella@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Lorenzo Isella
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...
Here is a simple approach using Data.Map: import qualified Data.Map as M substSubset :: (Num b, Ord a) => [a] -> [b] -> [a] -> [b] substSubset ys zs = map (\x -> M.findWithDefault 0 x (M.fromList $ zip ys zs)) First build a map from [0, 10, 50] to [2, 1, -5], then map a function over [0, 10 .. 50], which tries to find each corresponding number in the map with a default of 0. But do you really need zero values? If your zeroes actually represent lack of a value, consider using Maybe instead: [Just 2, Just 1, Nothing, Nothing, Nothing, Just (-5)] instead of: [2, 1, 0, 0, 0, -5] Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

FWIW here is another solution, using only lists. (Whereas I agree with
others who suggested using *proper* data structures rather than 'lists for
everything')
-- walks over both lists, always consumes an element from
-- the first list, consumes an element from the second list
-- only if values are equal.
ix :: Ord a => [a] -> [a] -> [b -> Maybe b]
ix (a:as) (b:bs) | a == b = Just : ix as bs
ix (a:as) (b:bs) | a < b = const Nothing : ix as (b:bs)
ix _ _ = []
-- consume a value from the second list, only if the current
-- function coming from the first list evaluates to a Just
proj :: [a -> Maybe b] -> [a] -> [Maybe b]
proj [] _ = []
proj (f:fs) (x:xs) =
let fx = f x
nextxs Nothing = x : xs
nextxs _ = xs
in fx : proj fs (nextxs fx)
*Main> proj (ix inp_a inp_b) inp_c
[Just 2,Just 1,Nothing,Nothing,Nothing,Just (-5)]
And of course, if you really want to substitute 0's for Nothings, you can do
it using something like:
to0s :: Num a => [Maybe a] -> [a]
to0s = map (fromMaybe 0)
*Main> to0s $ proj (ix inp_a inp_b) inp_c
[2,1,0,0,0,-5]
Hope this helps,
Ozgur
On 15 September 2010 00:35, Lorenzo Isella
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 2010-09-14, at 19:35 , Lorenzo Isella 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
Being a real Haskell newby, I can figure out a one-line solution in Python, but I don't know how to do something similar in Haskell, or even if it's possible. Please correct me if I'm wrong, but there does not seem to be a dictionary type in Haskell, and I am not aware of how to specify an inline if...else inside a list comprehension. I would really appreciate it if someone could show me how to do something similar to this Python statement in Haskell.
A=[0,10,20,30,40,50] B=[0,10,50] C=[2,1,-5] [dict(zip(B,C))[a] if a in B else 0 for a in A] [2, 1, 0, 0, 0, -5]
Henry

Henry Olders
Being a real Haskell newby, I can figure out a one-line solution in Python, but I don't know how to do something similar in Haskell, or even if it's possible. Please correct me if I'm wrong, but there does not seem to be a dictionary type in Haskell, and I am not aware of how to specify an inline if...else inside a list comprehension. I would really appreciate it if someone could show me how to do something similar to this Python statement in Haskell.
A=[0,10,20,30,40,50] B=[0,10,50] C=[2,1,-5] [dict(zip(B,C))[a] if a in B else 0 for a in A] [2, 1, 0, 0, 0, -5]
There is a dictionary type, but it has a more general name than in Python, which is quite common for Haskell data structures. It's called 'Map' and is defined in the Data.Map module. In general you want to import that module qualified to avoid name clashes: import qualified Data.Map as M Then you can use my solution from my other post, which is exactly equivalent to your Python solution: substSubset :: (Num b, Ord a) => [a] -> [b] -> [a] -> [b] substSubset ys zs = map (\x -> M.findWithDefault 0 x (M.fromList $ zip ys zs)) I prefer to use higher order functions rather than list comprehensions, because of better composability. But you can just as well use a list comprehension here: substSubset :: (Num b, Ord a) => [a] -> [b] -> [a] -> [b] substSubset ys zs xs = [ M.findWithDefault 0 x (M.fromList $ zip ys zs) | x <- xs ] The findWithDefault function from Data.Map does this comparison implicitly, but you can also do this instead: substSubset :: (Num b, Ord a) => [a] -> [b] -> [a] -> [b] substSubset ys zs xs = let dict = M.fromList (zip ys zs) in [ case M.lookup x dict of Just z -> z; Nothing -> 0 | x <- xs ] If course a more direct translation is also possible, but you really don't want that, because pattern matching is not only more elegant, but also faster and doesn't require an Eq constraint. Also note that your Python code does two lookups per element, while mine does only one. Anyway, it's really better to learn to use higher order functions properly. This greatly enhances both composability and code readability. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On Wednesday 15 September 2010 15:15:49, Henry Olders wrote:
On 2010-09-14, at 19:35 , Lorenzo Isella 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
Being a real Haskell newby, I can figure out a one-line solution in Python, but I don't know how to do something similar in Haskell, or even if it's possible. Please correct me if I'm wrong, but there does not seem to be a dictionary type in Haskell, and I am not aware of how to specify an inline if...else inside a list comprehension. I would really appreciate it if someone could show me how to do something similar to this Python statement in Haskell.
import Data.Maybe
A=[0,10,20,30,40,50] B=[0,10,50] C=[2,1,-5]
These have to be lowercase in Haskell, of course :)
[dict(zip(B,C))[a] if a in B else 0 for a in A]
map (fromMaybe 0 . (`lookup` zip b c)) a or, as a list comprehension, [fromMaybe 0 (lookup v dic) | let dic = zip b c, v <- a] Slightly more verbose than the Python. But this doesn't deal with multiple entries (istr that was mentioned previously in this thread), for a = [0, 10, 10, 20, 30 , 40, 50] b = [0, 10, 10, 50] c = [2, 1, 3, -5] neither would produce [2, 1, 3, 0, 0, 0, -5] which I believe would be the desired behaviour.
[2, 1, 0, 0, 0, -5]
Henry
participants (8)
-
Antoine Latter
-
Daniel Fischer
-
Ertugrul Soeylemez
-
Greg
-
Henry Olders
-
jean verdier
-
Lorenzo Isella
-
Ozgur Akgun