For what reason is it not allowed for an argument with the same name to appear more than once in the same equation? Examples:
f a a = show a
d && d = d
_ && _ = False
Technically speaking, formal parameters (The Report calls these variables.) are also patterns---it's just that they never fail to match a value. As a "side effect" of the successful match, the formal parameter is bound to the value it is being matched against. For this reason patterns in any one equation are not allowed to have more than one occurrence of the same formal parameter (a property called linearity §3.17, §3.3, §4.4.3).
According to PSR-1, class names should be in PascalCase, class constants should be in MACRO_CASE, and function and method names should be in camelCase.
Parameters in Haskell are rather reversed compared to imperative or object oriented languages. In an object oriented language, the object to work on is the very first parameter. In a function call it is often written even before the function name, say file in file.write("bla") .
The most basic way of defining a function in Haskell is to ``declare'' what it does. For example, we can write: double :: Int -> Int double n = 2*n. Here, the first line specifies the type of the function and the second line tells us how the output of double depends on its input.
According to this mailing list post, this was allowed in Miranda and was a common source of bugs: people would name two variables the same way by accident and have trouble discovering such a subtle mistake. So Haskell disallows it in favor of explicitly adding guards.
I personally think this is also much gentler on the poor reader of your code, who as a result of this rule need not keep in mind all the different variables that are in scope at the site of a pattern match to see whether the pattern will actually match everything or not.
You can't define
d && d = d
_ && _ = False
because you're trying to use pattern matching on a variable.
Pattern matching only works on data constructors, so
True && True = True
is OK, but
d && d = d
means "take the first argument (let's call it d
) and the second argument (let's call it d
) and..." but the compiler interrupts you saying
"no, wait, don't call them both d
, I won't know which one you mean!" and you say
"But I only want you to do this if they're the same anyway" and the compiler says
"That's not what names are for - test with a == b
for that. Please don't check for equality without using ==
, I don't know what you mean by equality unless you define ==
by making your data an instance of the Eq
typeclass, in which case, I'd like you to warn me in the type signature so I can get the correct definition for ==
compiled in."
When you say f a a = show a
, and then ask it to show 3 4
the compiler says "which one do you want me to show?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With