Syntactic Fructose
Syntactic Fructose

Reputation: 20124

passing a function with an arbitrary amount of parameters

I've recently been learning haskell from this awesome site, and I just learned about passing functions as parameters in the Higher Order Functions chapter. I can see how passing functions as parameters can be extremely useful, but from my current(lowley) understanding you need to specify the amount of parameters in this function, which can be quite limited given this function:

--takes a function and returns a function that has flipped arguments
flip' :: (a -> b -> c) -> (b -> a -> c) --very limited
flip' f = g
    where g x y = f y x

useful, but only to the point of functions taking two parameters, is there a way to write a function definition that accepts functions with an arbitrary amount of parameters? My guess would have to be some sort of parameter list, but I can't seem to find any information on it. Thanks!

Upvotes: 1

Views: 512

Answers (1)

zw324
zw324

Reputation: 27210

Actually flip works with functions with more than two parameters, but might not be exactly what you need:

Prelude> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
Prelude> :t flip (,,,)
flip (,,,) :: b -> a -> c -> d -> (a, b, c, d)

since

(,,,) :: a -> b -> c -> d -> (a, b, c, d)

is

(,,,) :: a -> (b -> (c -> (d -> (a, b, c, d))))

the first two parameters get flipped. Here a is a, b is b, but c is c -> d -> (a, b, c, d).

And then again, what do you expect to achieve by flipping a 3 parameter function like in this example?

EDIT: And also, there are vararg functions - one example is printf. This Haskell.org page is pretty nice. In short, some typeclass magic could be used to achieve this effect.

Upvotes: 8

Related Questions