Define a function with different amounts of arguments

Asked

Viewed 87 times

3

Is there any reason why this function:

foo f x y = f x y
foo x y = (+) x y

Return this error?

Equations for `foo' have Different Numbers of Arguments

Why doesn’t Haskell allow this behavior?

1 answer

3

Note that in the first case you have 3 arguments (f, x, y), and in the second you have 2 arguments (only x and y).

If you wanted to specialize the application of f, then it is better to create another function. Ex:

foosum x y = foo (+) x y
  • 4

    Or simply foosum = foo (+) (since the functions suffer curry by default).

  • I know they have different amounts of parameters. What I want to know is why it doesn’t work, the design decision that shaped this aspect of language.

  • the reason is to be able to use partially applied functions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.