1

Given this function (btw, should I say it's defined by cases? How do I refer to functions defined like this?),

f :: Int -> Int -> Int
f 0 x = x
f x _ = x

I'm wandering what is the reason, if one exists, why I cannot write it like this:

f :: Int -> Int -> Int
f 0 x = x
f = const

Indeed, upon trying to use this, I get this error

    Equations for ‘f’ have different numbers of arguments

which does seem obvious to me, as f = const is not incompatible with f taking two arguments (or any number of argumnts, fwiw; well, all functions take one argument and give back a function, right?).

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    Related to, if not a duplicate of: https://stackoverflow.com/questions/47059813/number-of-parameters-of-a-haskell-function – chi Aug 30 '20 at 18:35
  • @chi, totally right. I'm myself voting to close. – Enlico Aug 30 '20 at 20:00
  • 1
    Even though it would be *possible* to allow this, in practice, people write this by mistake more often than they try to do so on purpose, and there are good alternatives such as `LambdaCase` for pointfree definitions with multiple cases. For multiple arguments, you may like the pattern of combining `LambdaCase` with `curry`, e.g.: `curry \case { (0, x) -> x; (x, _) -> x }`. – Jon Purdy Aug 31 '20 at 01:33

1 Answers1

4

There's no fundamental reason why it can't be done. But it complicates the language, and there's no compelling reason to do it.

Li-yao Xia
  • 31,896
  • 2
  • 33
  • 56