Hi Roelof,

On May 3, 2014, at 3:09 AM, Roelof Wobben <rwob...@hotmail.com> wrote:

> Hello, 
> 
> Im now at the last exercise of the beginners exercise of 4clojure.

nice!

> 
> I figured out that this solution works.
> 
> (fn [default lijst1]
>   (loop [lijst lijst1 d {}]
>      (if (empty? lijst)
>        d
>        (recur (rest lijst) (assoc d (first lijst) default)))))

Yes, it passes the unit tests.

> 
> But as I see it lijst lijst 1 and d are all the same. They all contains the 
> collection which has to fill in the end collection.
> As a example I mean  default = 0 lijst 1 [:a :b: :c] 
> then lijst1 and d also contains [:a :b:c]  so what is then the point of using 
> 3 arguments.

There's only two arguments (to the loop and to the function). I re-wrote your 
code changing the names of variables and adding a few line breaks as:

(fn [default initial-keys]
  (loop [remaining-keys initial-keys
         result-map     {}]
     (if (empty? remaining-keys)
       result-map
       (let [key             (first remaining-keys)
             remaining-keys' (rest remaining-keys)
             result-map'     (assoc result-map key default)]
         (recur remaining-keys' result-map')))))

This also passes the unit tests.

Maybe that's a bit clearer as to what's going on. Maybe the new names help a 
bit. Notice the ticks on some of the names (the ' characters). This is just a 
convention some programmers use. The ticks are part of the name, nothing magic, 
and are used to indicate that the value of the name is derived from the value 
of the name with one less tick. Some programmers use numbers instead. Other 
programmers don't care and just use the same name.

When you've understood the solution you've come up with, you might want to try 
using reduce to do this. It'll be both shorter and much easier to understand. 
If you look carefully at your code the only part that isn't boiler-plate is the 
(assoc result-map key default) -- the boiler-plate corresponds closely to what 
reduce does.

I hope this doesn't confuse you even more.

Cheers,
Bob
 
> 
> Roelof
> 
> 
> -- 
> 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
> Note that posts from new members are moderated - please be patient with your 
> first post.
> 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
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to