I have two functions:
fun1 :: Int -> [Int]
fun2 :: [Int] -> [Int]
fun2 accept Int list and apply fun1 to each element of this list with help map. But fun1 return [Int]. So, I have type conflict. How to solve my problem?
You likely want a combination of map and concat to achieve it. Assuming fun1 and fun2 are something like this:
fun1 :: Int -> [Int]
fun1 x = [x,x]
fun2 :: [Int] -> [Int]
fun2 = map (+ 1)
solution :: [Int] -> [Int]
solution xs = concat $ map fun1 (fun2 xs)
Or as suggested by @CarstenKonig, you can use concatMap
solution2 :: [Int] -> [Int]
solution2 xs = concatMap fun1 $ fun2 xs
which can be further simplified to:
solution2 :: [Int] -> [Int]
solution2 = concatMap fun1 . fun2
fun2 = concatMap fun1 though - but yeah that's another one that type-checks ;)Another way is with list comprehension. Mapping fun1 over the list xs is
fun2' xs = [ fun1 x | x <- xs ]
-- = map fun1 xs
-- = do x <- xs -- for each x in xs:
-- return (fun1 x) -- yield (fun1 x)
which indeed would have a different type than what you wanted.
To flatten it one step further we do
fun2 xs = [ y | x <- xs, y <- fun1 x ]
-- = concatMap fun1 xs
-- = do x <- xs -- for each x in xs:
-- y <- fun1 x -- for each y in (fun1 x):
-- return y -- yield y
fun2 :: [Int] -> [[Int]]concatMapinstead ofmap(as infun2 = concatMap fun1) -concatMap fun1will applyfun1to each element in the input list but will flatten all the resulting lists into one by concatenating them