How can I multiply the elements of two lists in Haskell, two by two? Basically if I have [1,2,3] and [2,3,4] I want to get [2,6,12].
+19
A:
zipWith (*) [1,2,3] [2,3,4]
A useful way of finding a function such as zipWith
is Hoogle. There, you can enter in the type of the function you're looking for, and it will try to find matching functions in the standard libraries.
In this case your looking for a function to combine two lists of Int
s into a single list of Int
s using a combiner function (*)
, so this would be your query: (Int -> Int -> Int) -> [Int] -> [Int] -> [Int]. Hoogle will even find the correct funciton if you change the order of the arguments.
Tom Lokhorst
2009-05-04 09:31:11
More importantly, in this case (since the actual type of zipWith is (a -> b -> c) -> [a] -> [b] -> [c]), Hoogle will unify your query with any generic type parameters...
Chris Conway
2009-05-04 13:33:53
Right, that too.
Tom Lokhorst
2009-05-04 14:21:26