Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some functional languages not use parenthesis for function arguments? [closed]

I've been trying to a adopt a functional language into my programing. I use C# and rely heavily on LINQ and other functional constructs. Currently I'm focusing on Elm.

One thing that continues to bother me is the lack of parenthesis in functions. Math expressions use them:

f(z) = ln(z) + 1

Yet in many functional languages this is written as:

f z = ln z + 1

It this just a style thing or is there something deeper going here?

like image 280
Mike Ward Avatar asked Jul 07 '15 20:07

Mike Ward


People also ask

Why do functions use parentheses?

() (parentheses) Parentheses have multiple functions relating to functions and structures. They are used to contain a list of parameters passed to functions and control structures and they are used to group expressions to control the order of execution.

Why do functional languages not have loops?

A loop is not pure. Loops usually require variables, like a counter (so much impurity!). Purely functional programming languages, like Haskell, don't (by default anyway) provide ways to mutate a value, like a counter or a pointer. By default, variables are not mutable cells. Which in turn doesn't allow for loops.

Why do functional languages disallow side effects?

What I keep as conclusions, are that side effects do not affect the function value, due to equational reasoning, this is why "functional languages do not allow/minimize side effects". Effects embedded in the function values affect and change the state that is ever saved -or saved outside the core of the program.

Which brackets are used to give function arguments?

You can pass values over to your functions. These values are called arguments, and they go between the round brackets of the function. If you have more than one value to pass over, you separated them with commas.


1 Answers

Functional languages take root in lambda calculus, which do not use parentheses for functional application.

It is a syntactical thing sure, but the way that functions are applied it makes sense to leave parentheses out since a function with two arguments first applies the left most argument to the function and output a function that is used by the second argument. For example the function you are used to:

add(x,y) { return x + y } 

in functional terms is

add x y
add :: Int->Int->Int

where the intermediate step after applying x returns another function

add 4  -- returns a function that will return 4 + y, when y is applied
add 4 :: Int->Int

To a certain degree this helps avoid confusion with the programming languages you are used to where add(4) would throw an error. It reads more like math with associativity, ((add x) y).

like image 125
GleasonK Avatar answered Sep 29 '22 21:09

GleasonK