3 ms·
"if then else" is indicative of an underlying pattern that exists at a more fundamental level. In that sense, it was "discovered", not invented. Depending on
by bcheung 6y ago
"if then else" is indicative of an underlying pattern that exists at a more fundamental level. In that sense, it was "discovered", not invented.
Depending on the language, the syntax of "if then else" is not needed and is syntactic sugar.
In the Lambda Calculus you can implement "if else" using "first" and "second".
Here is something equivalent in JavaScript.
const trueFn = (first, second) => first
const falseFn = (first, second) => second
const ifFn = (boolFn, first, second) => boolFn(first, second)
ifFn(trueFn, 'yes', 'no') // returns 'yes'
ifFn(falseFn, 'yes', 'no') // returns 'no'
- samatman 6y agoNot truly equivalent! let's define `delete(file)`, which returns 0 if it successfully deletes the file, and 1 if there's an error. ifFn(trueFn, delete('A'), delete('B')) will not do what you want it to.
- bcheung 6y agoThis is true. In a lazy language it is the same, in imperative languages that do eager evaluation it will indeed have the problem you mention.