Loop and Recur

2010-06-07 Thread Jon Seltzer
I'm still learning Clojure and doing so by reading everything on clojure.org. I ran across this example in the Functional Programming section: (defn my-zipmap [keys vals] (loop [my-map {} my-keys (seq keys) my-vals (seq vals)] (if (and my-keys my-vals) (recur (assoc

Re: Loop and Recur

2010-06-07 Thread Steve Purcell
On 6 Jun 2010, at 15:30, Jon Seltzer wrote: I'm still learning Clojure and doing so by reading everything on clojure.org. I ran across this example in the Functional Programming section: (defn my-zipmap [keys vals] (loop [my-map {} my-keys (seq keys) my-vals (seq vals)]

Re: Loop and Recur

2010-06-07 Thread Steve Purcell
On 7 Jun 2010, at 12:43, Steve Purcell wrote: Empty seqs are logically true, so your if condition is always true. Apologies; I'm talking rubbish: user= (if '() (println truthy)) truthy nil user= (if (seq '()) (println truthy)) nil -- You received this message because you are subscribed to

Re: Loop and Recur

2010-06-07 Thread Bruce Durling
Steve and Jon, On Mon, Jun 7, 2010 at 12:43, Steve Purcell st...@sanityinc.com wrote: Empty seqs are logically true, so your if condition is always true. I was looking at that today too. I did ( 0 (count my-list)) in my if statement to fix it. Is the Recursive Looping example on

Re: Loop and Recur

2010-06-07 Thread Bruce Durling
Steve, On Mon, Jun 7, 2010 at 12:48, Steve Purcell st...@sanityinc.com wrote: On 7 Jun 2010, at 12:43, Steve Purcell wrote: Empty seqs are logically true, so your if condition is always true. Apologies; I'm talking rubbish: user= (if '() (println truthy)) truthy nil user= (if (seq '())

Re: Loop and Recur

2010-06-07 Thread patrik karlin
calling rest dosent give you nil it gives you an empty seq so the if statment never fails try (defn my-zipmap [keys vals] (loop [my-map {} [kf kr] (seq keys) [vf vr] (seq vals)] (if (and kf vf) (recur (assoc my-map kf vf) kr vr) my-map))) 2010/6/6 Jon Seltzer

Re: Loop and Recur

2010-06-07 Thread patrik karlin
So its the calling of first that gives you nil here is some example code user= (rest '(2)) () user= (rest '()) () user= (first '()) nil 2010/6/7 patrik karlin patrik.kar...@gmail.com: calling rest dosent give you nil it gives you an empty seq so the if statment never fails try (defn

Re: Loop and Recur

2010-06-07 Thread Stuart Halloway
Hi Bruce, That doc page used pre-1.0 Clojure code, which, as you saw, doesn't work. Thanks for the catch, I have fixed the docs on the site. Stu Steve and Jon, On Mon, Jun 7, 2010 at 12:43, Steve Purcell st...@sanityinc.com wrote: Empty seqs are logically true, so your if condition is

Re: Loop and Recur

2010-06-07 Thread Bruce Durling
Stu, On Mon, Jun 7, 2010 at 13:08, Stuart Halloway stuart.hallo...@gmail.com wrote: That doc page used pre-1.0 Clojure code, which, as you saw, doesn't work. Thanks for the catch, I have fixed the docs on the site. Thanks. I think my mistake was mixing up rest (which always returns a sequence

Re: Loop and Recur

2010-06-07 Thread Joost
On Jun 7, 1:49 pm, Bruce Durling b...@otfrom.com wrote: Steve and Jon, On Mon, Jun 7, 2010 at 12:43, Steve Purcell st...@sanityinc.com wrote: Empty seqs are logically true, so your if condition is always true. I was looking at that today too. I did ( 0 (count my-list)) in my if statement

Re: Loop and Recur

2010-06-07 Thread Bruce Durling
Melkel, On Mon, Jun 7, 2010 at 15:55, Meikel Brandmeyer m...@kotka.de wrote: Hi, On Jun 7, 4:25 pm, Bruce Durling b...@otfrom.com wrote: I have no problem with calling seq, I just don't understand why I need to. Because the initial collections might be empty. (my-zipmap [] []) = {} I