On Mar 4, 2009, at 4:43 AM, David Sletten wrote:

>
> On Mar 3, 2009, at 4:53 AM, Chouser wrote:
>>
>>> (defn roman? [roman-string]
>>>  (and (not (empty? roman-string))
>>>       (re-matches
>>>        #"(?:M{0,3})(?:D?C{0,3}|C[DM])(?:L?X{0,3}|X[LC])(?:V?I{0,3}|I
>>> [VX])$"
>>>        roman-string)))
>>
>> The normal idiom in Clojure is (seq x) instead of (not (empty? x)),
>> and that works fine for Strings.  Also, since you don't really need
>> the specific return value of the test expressions, 'when' might be a
>> better choice than 'and'.
>>
>
> Thanks for your comments Chris and Michel.
>
> I'll keep the "seq" idiom in mind.
>
> On the other hand, there is a convention in Common Lisp that I think
> is worth observing in Clojure as well. It relates to the distinction
> between "if" and "when". My view is that "if" is an expression, and
> expressions return values. This is true whether the test is true or
> not, so I am not a fan of implicit "else" expressions. Namely, I
> believe that "if" should always contain 3 subexpressions. Moreover,
> the "then" and "else" subexpressions by default consist of single
> expressions. Of course they could be wrapped in "do" forms, but they
> aren't automatically.
>
> Conversely, "when" says "side effect". This is clear for 2 reasons.
> First, there is no "else" clause, so "when" doesn't really work as an
> expression. Furthermore, the forms in the "when" body are evaluated
> in an implicit "do". Since only the final value is returned, any of
> the earlier expressions in the "when" body are useful only for their
> side effects.
>

This is not the convention in Clojure for a couple of reasons.

First, there are far fewer side effects. 'when' does not say side  
effects, although as you point out, it does support them. But then, so  
do function bodies and they don't inherently say side-effects.

I agree 'if' should always have 3 subexpressions, all the more reason  
to use 'when' when the second is going to be nil. Idiomatic Clojure  
code is full of expressions like:

(when there-is-stuff (expr stuff))

Which I read as when there is stuff, some function of it, else  
nothing. There's nothing inherently side-effecting about it.

It would certainly be a waste of 'when' in Clojure to reserve it for  
side effects, since so much code contains none.

Rich


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to