Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argument naming rule in haskell

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).

like image 959
t0ma5 Avatar asked Dec 17 '13 16:12

t0ma5


People also ask

Should function names be camelCase?

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.

What is a parameter in Haskell?

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") .

How are functions defined in Haskell?

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.


2 Answers

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.

like image 90
Daniel Wagner Avatar answered Oct 25 '22 20:10

Daniel Wagner


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?"

like image 45
not my job Avatar answered Oct 25 '22 19:10

not my job