I am very excited about opportunities of what I can do in Clojure. I am
a long time Squeaker (Smalltalk), and also Python user, dabbling at
times in various other languages. I have briefly explored Scala as an
option for my business app. But, I really am a dynamic typing guy. :)
I have in general not learned Lisp previously because I was spoiled by
the environment in Squeak which is not found, in general, outside of
Smalltalk. However, I am writing a business application which requires
using Java libraries. Hence my search for a JVM language which makes
using Java less painful and more productive. I have great hope for
Clojure being that language. It being a Lisp is a big win to me. The
power of Lisp is known, even among those who don't use it. I absolutely
love the fact that Rich explicitly wants a practical Lisp that is
available for writing business apps on the platform chosen for many
business apps. That hits me where I live.
I am working my way through the Programming Clojure book. I really want
to get Clojure in my head and get productive with it.
On page 67 is-small? is defined as:
(defn is-small? [number]
(if (< number 100) "yes" "no" ))
Is is-small? a predicate? If so, is this a common pattern for such
predicates?
The reason I ask, is because on page 52 a predicate is defined as:
A predicate is a function that returns either true or false. In
Clojure, it is
idiomatic to name predicates with a trailing question mark, for example
true?, false?, nil?, and zero?:
If this is strictly true, then is-small? is not a predicate as it
returns values other than true or false. And as such could not be used
in a situation which required such.
Just attempting to clarify my understanding, so that I proceed
correctly, with proper understanding.
Thanks for any wisdom, understanding and guidance in my journey to Clojure.
Jimmie
Am 21.07.2009 um 22:48 schrieb Jimmie Houchin:
> (defn is-small? [number]
> (if (< number 100) "yes" "no" ))
>
> Is is-small? a predicate? If so, is this a common pattern for such
> predicates?
The definition is correct. is-small? is not
a predicate. It returns a string. So (if (is-small? x) ..)
is always true! I suspect that the string
produced by is-small? is used somewhere.
(defn is-small?
[number]
(< number 100))
This would be a predicate since it returns
a Boolean.
Hope this helps.
Sincerely
Meikel
Thanks for the information.
That is what I thought. Is it proper or idiomatic Clojure to use a "?"
symbol on non-predicate functions?
That is somewhat what prompted my question. If a "?" symbol is
generally, always or primarily reserved for predicates, it just seemed
strange to use a non-predicate example in the book. Especially when the
example could just as easily used true or false, instead of yes or no.
Thanks again.
Jimmie
I don't think so. The standard library doesn't, at any rate.
Note that predicates don't necessarily have to return literal true or
false: in my opinion at least, it's perfectly reasonable to write
(def my-predicate? #{:foo :bar})
-- it'll behave correctly in if and when, but the return value will
actually be a keyword or nil, not true or false.
> That is somewhat what prompted my question. If a "?" symbol is
> generally, always or primarily reserved for predicates, it just seemed
> strange to use a non-predicate example in the book. Especially when
> the
> example could just as easily used true or false, instead of yes
> or no.
Certainly seems odd to me.
Am 22.07.2009 um 19:35 schrieb Richard Newman:
> Note that predicates don't necessarily have to return literal true or
> false: in my opinion at least, it's perfectly reasonable to write
>
> (def my-predicate? #{:foo :bar})
>
> -- it'll behave correctly in if and when, but the return value will
> actually be a keyword or nil, not true or false.
Be careful with this. Only do this if you know
in advance, that neither false nor nil are in
your set. To be safe use contains?, which makes
the whole thing again a predicate.
(defn my-predicate?
[elem]
(contains? set-I-have-not-full-control-over elem))
Sincerely
Meikel
I was just looking to clarify my understanding and didn't understand the
conflict between the definition and example.
Unless "?" was idiomatic for things other than predicates.
Thanks for the clarification.
I have yet to encounter the situation of being condemned to an extra
glass of wine being punishment. Hmm.
Jimmie