News for hackers. Stuff that matters.
Re:Optional If Expressions by SeanLuke on Saturday April 15, 2023 @01:58AM (#35578006)
So.... (if (/= den 0.0) (/ num den))

Languages keep reinventing Lisp. Greenspun's 10th rule.

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?
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.

Re:Optional If Expressions by lispm on Saturday April 15, 2023 @03:47AM (#35578541)
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
Re:Optional If Expressions by haileys on Saturday April 15, 2023 @03:14AM (#35578387)
Would this not just return nil for the else case? This is already the behaviour of Ruby and Lua as well, of course both are unityped languages where "just return nil" semantics are the natural conclusion.

The OP however is discussing how this kind of thing would work in a language with a richer type system, like Rust.

Re:Optional If Expressions by SeanLuke on Saturday April 15, 2023 @07:34AM (#35579664)
I guess the Lisp example is an example of a nullable type, which he's railing against; he'd rather have prewrapped types like Some(result) vs None. But it's still an optional expression, and I'm not convinced that declaring an optional type, doing the expression(s), then unwrapping and checking the result for None, is any better than just using an exception handler. He insists that nullable types force you to check for null, but they don't, at least in his math example here. Just let the runtime do it and throw an exception.
Re:Optional If Expressions by f_devd on Saturday April 15, 2023 @08:02AM (#35579823)
> Just let the runtime do it and throw an exception.

Afaik there are no exceptions in rust only panics & `Result`s (and `Option` I suppose)

Re:Optional If Expressions by eyelidlessness on Saturday April 15, 2023 @01:04PM (#35582410)
It will typically return nil, but it’s worth noting that in some lisps nil is the same as the empty list (). Not quite the same as Option/None, but conceptually more similar than most other languages’ null types.
Re:Optional If Expressions by Sharlin on Saturday April 15, 2023 @04:03AM (#35578625)
That’s a bit like saying that all languages with an imperative for loop keep reinventing FORTRAN. Although I give you that many of Lisp’s good ideas were unfashionable for a while, reusing those ideas isn’t in itself an “ad hoc” implementation of Lisp.