Jonatan Matias Vaara
Jonatan Matias Vaara

Reputation: 23

Haskell: Sending parameters to multiple functions

I am a beginner to Haskell. This is an example from my lectures, where we define our function permu which should produce all permutations of a list.

permu :: [a] -> [[a]]
permu [] = [[]]
permu (x:xs) = concatMap (insertAll x) $ permu xs
insertAll :: a -> [a] -> [[a]]
insertAll = undefined

I'll leave insertAll as undefined for now, as it is not the part that I need help with. My problem is this: concatMap has the type Foldable t => (a -> [b]) -> t a -> [b] and should thus take two parameters. However, insertAll should also take two parameters.

As far as I can tell concatMap takes insertAll x as first parameter and permu xs as the second. This all is good. But it looks to me like insertAll only takes the argument x.

Is it possible that both concatMap and insertAll takes permu xs as their second parameter, or am I missing something?? Thank you!

Upvotes: 2

Views: 130

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

But it looks to me like insert all only takes the argument x.

As the comment mentioned, Haskell functions are curried by default. This makes partial application of them easy - simply providing less arguments than the function "takes" leaves you with a partially applied function that will take the rest.

In your case, partially applying insertAll on x leaves us with a [a] -> [[a]] function, which makes the signature compatible with the first argument of concatMap. The second argument to insertAll is effectively provided within concatMap.

Upvotes: 3

Related Questions