Open AlexanderChenNL opened 6 years ago
:) hi here is the iff
doc: http://en.wikipedia.org/wiki/If_and_only_if
And this is the answer: (fn [a b] (nil? (b a 0)))
Hi,
I got some lost at this question (got already read that about the iff
table linked above). I feel instead, info about the (nil? [x])
function or usage (e.g. in this case (as I see it used here as a kind of side effect)) would be of great help but not found so far..
So, something is missing in this one (the 26/32)
. As the exercise query reads as follow:
Write an anonymous function which, given a key and map, returns true iff the map contains an entry with that key and its value is nil.
My answer (& I find that (the) [other|previous] answer answers other query) is
(fn [a b] (= nil (#_get a b)))
or (fn [a b] (nil? (#_get a b)))
;; (also got that it's just too close to literals but still under [training|beginner]
;; purposes though I'm currently unable of testing the first condition of the exercise,
;; the `and`
;; for whether or not
;; the given `key` exist in the given `coll` bit )
OK I found this another answer:
(fn [%1 %2] (and (contains? %2 %1) (nil? (%2 %1))))`
%1
%2
for referencing arguments in the function body.and
wrapping function (as in this case was specially sine qua non).I still do not know how-to these other solutions:
(fn [a b] (nil? (b a 0))) ;; posted above
or this another one, work:
#(nil?(%2 %1 0))
Any explanation would be so greatly appreciated.
hi @touxstone , thanks for reaching out. that's a tricky one :)
It should return true if two things are true. That the collection contains the key, and the key is nil
b
indicates the map
a
indicates the key
and the third argument which is 0
is the default value when (b a)
returns nil so (:key {:bb 1} 0)
will produce 0
. if it was like that (:key {:bb 1})
it returns nil
.(this the argument for the last test assertion in the site)
I hope this helps if not please let me know I'll elaborate as much as I can.
Hi, @ertugrulcetin just a short reply —Ah ha!
Thank you, so much!
;; Though, I'd highlight (got it in my priv notes) this exercise as:
;; smart usage of `the default value functionality` e.g. for `get`
;; (& probably some other primitive functions)
I for the live of me don't understand what the assignment is about, please help :)