3 ms·
Making "if" a special form means that it can short-circuit, and I'd rather write (if x big_expensive_expression y) than (funcall (if x (lambda () big
by geocar 16y ago
Making "if" a special form means that it can short-circuit, and I'd rather write
(if x big_expensive_expression y)
than
(funcall (if x (lambda () big_expensive_expression) (lambda () y)))
- deleted 16y ago[deleted]
- deleted 16y ago[deleted]
- geocar 16y agoAre you familiar with python? Perhaps this will help: if cnd: return do_big_stuff else: return do_other_big_stuff has different semantics than a hypothetical "if" function call: def a(): return do_big_stuff def b(): return do_other_big_stuff return if(cnd,a(),b()) Note here, both "do_big_stuff" and "do_other_big_stuff" are evaluated, regardless of the value of "cond". A short-circuiting if (only possible if "if" is a special form) will ONLY evaluate a() (and thus "do_big_stuff") if "cond" returns a truth-value. Now, Factor's "if" works like this (in python): return if(cnd,a,b)() That's fine- and it still has the short-circuiting semantics, but there's still a question of taste here.
- Avshalom 16y agoYeah okay I got it. I think the funcall/lambda stuff got me confused, it's kind of obvious in hindsight. In which case I agree it's a matter of taste. I guess I just like the taste of Factor/REBOL always passing blocks to if, having to say t [ 0 ] [ 3 ] if instead of simply say t 0 3 if is the trade off I suppose[1]. I'm guessing it works for picolisp because it's purely interpreted? because it certainly doesn't seem to be passing lambdas to 'if'. Can anyone confirm/deny? [1]well actually the word ? does that but to make it work for both you need to do: : myif ( ? true false -- ) ? dup quotation? [ call ] [ ] if ; inline
- geocar 16y agoIt works in picolisp because the author foolishly implemented FEXPRs instead of special forms, and rather than fix this problem, spends time trying to convince people that this is actually a feature, and that it offers a certain succinctness that can't be achieved in other ways.
- gecko 16y agoYou mind it mostly because the Lisp funcall/lambda syntax is complex. Contrast with Smalltalk, which lacks special forms, and does use lambdas to get short circuiting: someBoolean ifTrue: [ 'it was true' ] ifFalse: [ 'it was false' ] Brackets introduce lambdas. I don't find this syntax clunky, and while I've never wanted to map ifTrue:/ifFalse: before, it's nice to be able to write your own special-form-like operators this way.
- geocar 16y agoFactor and Ruby do the same thing. I agree that there's definitely value in having dedicated closure syntax.
- anonymousDan 16y agoWhat about for lazy languages such as Haskell? I guess you get short circuiting by default there no?