DEV Community

Cover image for Does the functionality of Grouping operator () in JavaScript differ from Haskell or other programming languages?
Ken Okabe
Ken Okabe

Posted on

Does the functionality of Grouping operator () in JavaScript differ from Haskell or other programming languages?

Grouping operator ( ) in JavaScript

The grouping operator ( ) controls the precedence of evaluation in expressions.

does Not differ from Haskell or other programming languages. (there is a minor exception as a byproduct of their main purpose in Lisp/Clojure)

In other words, grouping operator ( ) in every language shares the common functionality to compose Dependency graph

In mathematics, computer science and digital electronics, a dependency graph is a directed graph representing dependencies of several objects towards each other. It is possible to derive an evaluation order or the absence of an evaluation order that respects the given dependencies from the dependency graph.

and the functionality of Grouping operator ( ) itself is not affected by a evaluation strategy


Perhaps we can share the code below:

  • f() * (g() + h())

to discuss the topic here, but not limited to the example.


Haskell

In the code in Haskell where the evaluation strategy is Lazy / Call-by-need

https://en.wikipedia.org/wiki/Lazy_evaluation


In this code, according to the call-by-need, the evaluation order of f g h is

h(1) -> f(1) -> g(1)


In this code, according to the call-by-need, the evaluation order of f g h is

f(1) -> g(1) -> h(1)

JavaScript

//function definition  
const f = a => a + 1;  
const g = a => a + 2; 
const h = a => a + 3; 

//calling functions 
console.log( 
(f(1) * g(1)) + h(1) 
); //10 
console.log( 
f(1) * (g(1) + h(1))   
); //14 
Enter fullscreen mode Exit fullscreen mode

In this code, according to the eager-evaluation the evaluation order of f g h is

f(1) -> g(1) -> h(1)

in both cases.


Either way, the regardless the evaluation strategies, since the dependency graph composed with Groping operator() and + * stays identical, the final evaluated value is also identical.

Top comments (1)

Collapse
 
kentechgeek profile image
Ken Okabe • Edited

StackOverflow Question of the same title:
Does the functionality of Grouping operator () in JavaScript differ from Haskell or other programming languages?
stackoverflow.com/questions/693846...

Check out one of the detailed great answer:
stackoverflow.com/a/69386130/11316608

or you can join the Q&A.