Let's talk about the if
statement. A lot of people view it as the corner-stone of programming, for example, whenever I discuss the question if CSS is a programming language or not (it is) somebody would say: "CSS doesn't have if
statement so it can't be considered as PL". I don't know where this idea comes from. if
statement is one of the many control flow structures, like goto, jump, exceptions, loops, etc.
Procedural
I guess the most known form of if
is it's "procedural" or structured form:
if (condition) {
thenAction();
} else {
elseAction();
}
By "procedural" I mean the type of imperative programming, but more organized than just Von Neumann machine. (Imperative style of programming is when you give direct instructions to the machine do step 1, step 2, etc.)
Instead of the precise definition, I will give examples:
- Most imperative languages: assembly (machine code), Fortran.
- Less imperative languages, so-called "procedural" or structured): Algol, Pascal (I guess Go as well).
Functional
To impelemnt if
in functional style we need to restrict ourselves to functions only:
// Implementation:
const True = (x) => (y) => x;
const False = (x) => (y) => y;
const If = (condition, thenAction, elseAction) => {
const action = condition(thenAction)(elseAction);
return action();
}
// Usage:
If(condition, thenAction, elseAction);
This idea is a direct translation of Church Encoded True
, False
in lambda calculus:
True = λx.λy.x
False = λx.λy.y
Object-oriented
To impelemnt if
in functional style we need to restrict ourselves to objects only:
// Implementation:
class TrueClass {
Then(callBack) {
callBack.call()
return this
}
Else(callBack) {
return this
}
}
class FalseClass {
Then(callBack){
return this
}
Else(callBack){
callBack.call()
return this
}
}
const True = new TrueClass();
const False = new FalseClass();
// Usage:
// const condition = True or False
condition.Then(thenAction).Else(elseAction)
Conclusions
In practice, a lot of OOP and FP languages use procedural-style if
(you can get away without it).
The reason I presented those examples is to show "pure" FP and OOP implementations so you could compare them. For the full picture, I need to mention that OOP example is SmallTalk style, and SmallTalk took some inspiration from Scheme, which in turn uses ideas from lambda calculus ¯\_(ツ)_/¯
. Some people would argue definition of "pure" OOP as SmallTalk, let's leave it for another article.
Photo by Tom Parsons on Unsplash
Top comments (9)
I think you forgot one. I'm not sure what to call it. Perhaps "calculated". The language is this case is tbas, a dialect of BASIC.
Inside that
for/next
there's anif
hidden away in the calculation.Many dialects of BASIC do not have a
boolean
type but rather use the values0
and-1
to signifyfalse
andtrue
.In the equation above the
n %% i = 0
takesn
modulusi
and tests the result against0
. If true,-1
otherwise0
. The next level up in the formula therefore multipliesi
by either-1
or0
resulting in either-(i)
orzero
. The next level up subtracts a negative or subtracts a zero. According to standard number theory, subtracting a minus value is the same as adding a positive.Were one to use
if/then
here it would convertto
In tbas's case, the formulaic approach is faster than the
if/then
. YMMV.To be fair I didn't try to least them all (if it's even possible, how to guarantee the exhaustiveness of list). Nice to have more examples
Thanks for the article.
Sometimes I think inside myself, how come new programmers (esp. bootcampers) ignore such information just cuz they're fixated on one lang that "does the job" for them 😕
I can try to answer your question.
Would you be happy to learn Lisp? (I guess that average answer would be no)
But Lisp is very powerful for learning because it overcomes "tyranny of syntax" (when people focus on syntax instead of the actual meaning, when they can't unsee the syntax).
I didn't start from learning Lisp. I learn it now, by implementing Lisp interpreter. And I realize how much-overcomplicated solutions I dealt with through my career. How much confusing explanations I saw, because people don't understand what they talk about (I did it myself, I thought I knew something. I was wrong).
PS I'm not using Lisp for work and doubt I will use it professionally. I use it as a learning vessel.
Exactly, I hardly know people who love the idea of mere learning rather than "doing their job".
I would say it's really a deep personal philosophy more onto: do I really wanna know what I'm doing or not?
I took Lisp and Haskell in my uni, wasn't happy about learning it till I saw the potential of understanding what actually functional means.
Very interesting but, could you... Uhh... ELI3?
Can you show some examples of using the fp and oo definitions?
Or imagine any function which would return our predefined values
True
orFalse
.Awesome, thanks.
The conversion was the tricky part... looks obvious now that you showed it.
Yeah... They idea is to imagine language wehere instead of
true/false
(0/1
) they useTrue/False
(those which I provided). It is hard to wrap head around in JS. But I have series of posts about creating a language, in this language we can:if
if
and implement functionalif
This should be more clear.