3 ms·
It's been a long time since I used common lisp, but I seem to remember that the convention was to use if in a conditional expression and when for something that
by arethuza 7y ago
It's been a long time since I used common lisp, but I seem to remember that the convention was to use if in a conditional expression and when for something that conditionally produces a side effect?
- diggan 7y agoIf it's anything like Clojure (big assumption, but skimming the submission link seems to confirm things are the same), `if` is used when you want to do something OR do something else. You use `when` only when there is no `OR do something else` and/or when there is multiple things you want to do inside.
- pvinis 7y agoIt's interesting to see how you use your if and when in your sentences. Surprisingly, you have an if without an else, and a when with an or at the end! :p I do remember that convention though, and it makes sense. I just found your usages in text interesting.
- diggan 7y agoHaha, you're right! Re-reading my own comment, I seem to switch context between "normal language" conditionals where the rules obviously are different from the "programming language" context where they are strictly defined. Fun observation :)
- aidenn0 7y agoWhen has no else and if has no explicit progn. If you need an else for a side effect then cond or if will be used. I don't think there is a consensus on which of those to use for side effect vs expression when an else is needed
- arethuza 7y agoI probably got that idea from CLtL: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node84.html https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node84.html Very much a "stylistic preference" though! Edit: I realised that for the 6 years or so I wrote CL as part of my day job that my well thumbed copy of CLtL was by far the most referenced programming language book I have ever used (mostly falling open at either the loop or CLOS chapters). Not sure what this says about CL!
- reikonomusha 7y agoThis is a style rule at Rigetti Computing: use IF or COND when the return value matters and no side effects; use WHEN, UNLESS, COND if you have side effects. (Never use IF for side effects, never use WHEN/UNLESS for return values.)