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].
From stackoverflow
-
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 ofInt
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.Chris Conway : 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...Tom Lokhorst : Right, that too.
0 comments:
Post a Comment