This isn't really a Clojure specific question but is there any benefit to making 'if' a special form? Meaning you can't do: (map if [true false true] [1 1 1] [2 2 2]) not that I can think of any reason why you'd want to do that (specifically) but Picolisp, Factor, REBOL and I'm sure a couple other languages implement if as a normal function. To take an example from some edition of programming clojure: (defmacro unles…
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)))
Try Clojure
51–60 of 63 posts
Re: Try Clojure
#52This isn't really a Clojure specific question but is there any benefit to making 'if' a special form? Meaning you can't do: (map if [true false true] [1 1 1] [2 2 2]) not that I can think of any reason why you'd want to do that (specifically) but Picolisp, Factor, REBOL and I'm sure a couple other languages implement if as a normal function. To take an example from some edition of programming clojure: (defmacro unles…
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)))
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.Re: Try Clojure
#53Re: Try Clojure
#54Earlier quoted context omitted.
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]
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.Re: Try Clojure
#55Earlier quoted context omitted.
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)))
You 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 operator…
Re: Try Clojure
#56Nice work. Assuming you're trying to advocate Clojure, I'd recommend changing your tutorial to make things look easy (as opposed to highlighting gotchas). I recommend doing math examples first. Show how it makes a good calculator, but avoid discussion of ratios. That's (unnecessarily) confusing.. just like everything in the hello world example starting from the nil output on. By the time I got to the sequence page, I…
I don't know if you noticed, but I tried to be very explicit about the fact that the tutorial is nowhere near finished. I wrote that up in 1 hour yesterday. I'm well aware that it needs filling in with less difficult stuff. I intend to start serious work on it today. Also, I'm trying to get Michael Fogus (author of Joy of Clojure) to write the tutorial. If he decides to do so, I can almost promise you quality. ;)
Re: Try Clojure
#57Earlier quoted context omitted.
Well, it's actually impossible to write if as a function in Clojure.
I think it's impossible to write if as a function in anything, I'm pretty sure that at some point you need a branching primitive, but the if the VM uses and the if exposed to the user don't have to be the same thing. Like I said though, it's not a Clojure specific question, CL and Scheme both use a special form for if as well. Even if it can't be done in a JVM language it can be done. I'm just curious if there's any…
Re: Try Clojure
#58Earlier quoted context omitted.
[deleted]
Are 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…
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
Re: Try Clojure
#59This isn't really a Clojure specific question but is there any benefit to making 'if' a special form? Meaning you can't do: (map if [true false true] [1 1 1] [2 2 2]) not that I can think of any reason why you'd want to do that (specifically) but Picolisp, Factor, REBOL and I'm sure a couple other languages implement if as a normal function. To take an example from some edition of programming clojure: (defmacro unles…
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)))
Re: Try Clojure
#60Earlier quoted context omitted.
I don't know if you noticed, but I tried to be very explicit about the fact that the tutorial is nowhere near finished. I wrote that up in 1 hour yesterday. I'm well aware that it needs filling in with less difficult stuff. I intend to start serious work on it today. Also, I'm trying to get Michael Fogus (author of Joy of Clojure) to write the tutorial. If he decides to do so, I can almost promise you quality. ;)
I just meant to be offering constructive feedback. The tutorial was useful for me. It's certainly way better than the tutorial I had before you wrote it! It's pretty impressive for an hour of work.
I'm not entirely certain I'm qualified to write a Clojure tutorial anyway, which is why I hope Michael is interested.