News for hackers. Stuff that matters.
Re:If-then-else had to be invented by bcheung on Sunday December 13, 2020 @12:40PM (#25408338)
"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'
Re:If-then-else had to be invented by samatman on Sunday December 13, 2020 @07:20PM (#25411928)
Not 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.

Re:If-then-else had to be invented by bcheung on Monday December 14, 2020 @03:13PM (#25422615)
This 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.