tags:

views:

430

answers:

1

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 Ints into a single list of Ints 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
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
Right, that too.
Tom Lokhorst