combine-function with list-comprehension
haskell, list-comprehension
Solution
What you need is a parallel list compehension. To be able to use it you need to specify a `ParallelListComp` pragma to the compiler:
{-# LANGUAGE ParallelListComp #-}
combine :: (a -> b -> c) -> [a] -> [b] -> [c]
combine f xs ys = [ f x y | x <- xs | y <- ys ]
Compiler desugars it to an application of `zipWith`:
combine :: (a -> b -> c) -> [a] -> [b] -> [c]
combine f xs ys = zipWith f xs ys
Which is actually what your function is, so:
combine :: (a -> b -> c) -> [a] -> [b] -> [c]
combine = zipWith
Problem
I've a simple question: ``` combine :: (a -> b -> c) -> [a] -> [b] -> [c] combine f (a:as) (b:bs) = f a b : combine f as bs combine _ _ _ = [ ] ``` This is recursive. Now i want to use a list comprehension to solve the same problem: ``` combine f (x:xs) (y:ys) = [ f x y | x <- (x:xs), y <- (y:ys) ] ``` But my problem is the combination of elements. I only want to combine `x1 y1, x2 y2, xs ys ...` not `x1 y1, x1 y2, x1 ys, x2 y1, x2 y2, .....`. Thank you!