DEV Community

Discussion on: Practical Functional Programming in JavaScript - Control Flow

Collapse
 
iquardt profile image
Iven Marquardt

FP has control flow, but one that is rather determined by arithemtic laws and non-strict evaluation than the lexical position of statements/expressions. An operator can be associative, commutative, distributive. Monoid, for instance, is associative, but not commutative, which gives you a precise and predicable control flow. The entire Monad type class exists to compose statements and thus allow modelling control flows similar to those in imperative programming.

Collapse
 
richytong profile image
Richard Tong

Doesn't control flow imply statements? My understanding is pure functional programming languages do not have statements and control flow is an imperative only construct.

Collapse
 
iquardt profile image
Iven Marquardt • Edited

Consider the following computation:

readFromUser
logUserInput

You need an order for that. Another example:

// dropping the parenthesis for clarity
f g x

// adding parenthesis to reveal left associativity of function application
(f g) x

Function application establishes a left associative evaluation order. Function composition establishes a right associative one.

With monads you define evaluation order of function composition with side effects (first example).

Now if you refer to the purely functional theory (lambda calculus) you are almost right: There are different evaluation strategies and the compiler decides which one is applied.

You can even write: Loosely speaking there is no evaluation order in FP, but this is a simplification.