Re:Optional If Expressions
by valenterry on Saturday April 15, 2023 @02:11AM
(#35578064)
Was Lisp really the first language where you could provide a custom if-expression or have a builtin if-expression?
[ Parent
| on HN ]
Re:Optional If Expressions
by bheadmaster on Saturday April 15, 2023 @02:49AM
(#35578248)
As far as I know, Lisp was the first expression-oriented language, so the if-as-an-expression idea probably comes from there.
In contrast to Fortran, in which "if" was a strict statement, which doesn't evaluate to anything and cannot be used directly in assignment.
FORTRAN I and II then had only an Arithmetic IF statement with three labels to jump to, based on an arithmetic value ( https://en.wikipedia.org/wiki/Arithmetic_IF ).
FORTRAN example:
IF ( N ) 10, 20, 30
Above means: if N is less than zero, then jump to label 10
if N is zero, then jump to label 20
if N is greater than zero, then jump to label 30
LISP had a COND expression, with multiple condition clauses. An example with three clauses. (COND ((= x 1) 'one)
((= x 2) 'two)
(T 'not-one-or-two))
if X is 1, then return ONE
if X is 2, then return TWO
ELSE return NOT-ONE-OR-TWO
John McCarthy mentions conditional expressions in this paper from 1958: https://www.softwarepreservation.org/projects/LISP/MIT/AIM-0...In external LISP syntax a COND would have looked similar to this:
(x>1 -> 1, x=0 -> 0, x<1 -> -1)
Above would mean: if X greater than 1, then return 1
if X is 0, then return 0
if X is smaller than 1, then return -1