I am working through some Clojure tutorials and one of the problems is: Implement (boolean x), which works like the built-in boolean function: for nil and false, it returns false, and for all other values it returns true. You can use if in its implementation, but not the build-in boolean.
I cannot figure out how to do this with just the if statement. This works, but is there a better way?
_Module
答案 0 :(得分:4)
gradlew assembleProdStaging
gradlew crashlyticsUploadDistributionProdStaging
and nil
are both 'falsey' (i.e. are equivalent to false
in a boolean context) so you can do:
false
(defn boolean [x]
(if x true false))
expressions are of the form if
so if (if condition true-expr false-expr)
evaluates to condition
the entire expression evaluates to true
otherwise true-expr
. So if false-expr
is x
(i.e. not truthy
or nil
) the expression will evaluate to false
. If true
is x
(falsey
and nil
are the only falsey values in clojure) then false is returned.
答案 1 :(得分:1)
maybe like this?
[Authorize]