Re: Baffled by NPE

2009-11-05 Thread John Harrop
On Thu, Nov 5, 2009 at 3:53 PM, Meikel Brandmeyer wrote: > Hi, > > Am 03.11.2009 um 22:41 schrieb jan: > > > (defn for-each [f items] > > (when-not (empty? items) > > (f (first items)) > > (recur f (rest items > > And to not defeat the learning exercise... > > (defn for-each >

Re: Baffled by NPE

2009-11-05 Thread Meikel Brandmeyer
Hi, Am 03.11.2009 um 22:41 schrieb jan: > (defn for-each [f items] > (when-not (empty? items) > (f (first items)) > (recur f (rest items And to not defeat the learning exercise... (defn for-each [f items] (when-let [s (seq items)] (f (first s)) (recur f (next s)

Baffled by NPE

2009-11-03 Thread Dean Wampler
I'm working the exercises in SICP using Clojure and learning Clojure as I go. I've stumbled across a mystifying NullPointerException. I'm implementing exercise 2.23, where you're supposed to implement a "for-each" method that takes a function and a list of items. It applies the function to teach it

Re: Baffled by NPE

2009-11-03 Thread James Reeves
On Nov 3, 9:41 pm, jan wrote: > When you only have a single branch in `if' it is more conventional to > use `when', or in this case `when-not'. And defn sets up a recur point > so there's no need for the explicit loop either. I thought "when" was mainly used for side-effects? "if" seems more app

Re: Baffled by NPE

2009-11-03 Thread Brian Hurt
On Tue, Nov 3, 2009 at 4:22 PM, Brian Hurt wrote: > > > On Tue, Nov 3, 2009 at 4:21 PM, Dean Wampler wrote: > >> Ah, of course. Thanks. This works: >> >> (defn for-each [f items] >> (if (not (empty? items)) >> (let [] (f (first items)) (for-each f (rest items) >> >> > Or: > > (defn fo

Re: Baffled by NPE

2009-11-03 Thread Brian Hurt
On Tue, Nov 3, 2009 at 4:21 PM, Dean Wampler wrote: > Ah, of course. Thanks. This works: > > (defn for-each [f items] > (if (not (empty? items)) > (let [] (f (first items)) (for-each f (rest items) > > Or: (defn for-each [ f items] > (for-each (fn [x] (println (* x x))) (list 1 2 3

Re: Baffled by NPE

2009-11-03 Thread Timothy Pratley
Also Clojure has doseq which defeats the learning exercise but nets a nicer golf score of 14: (defn for-each [f items] (doseq [i items] (f i))) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To pos

Re: Baffled by NPE

2009-11-03 Thread jan
Brian Hurt writes: > Of course, you don't have tail call optimization in Clojure (dang jvm).  So > this is probably better: > (defn for-each [f items] > (loop [ curr items ] > (if (empty? curr) > nil > (do > (f (first curr)) > (

Re: Baffled by NPE

2009-11-03 Thread Dean Wampler
Ah, of course. Thanks. This works: (defn for-each [f items] (if (not (empty? items)) (let [] (f (first items)) (for-each f (rest items) (for-each (fn [x] (println (* x x))) (list 1 2 3 4 5)) On Tue, Nov 3, 2009 at 3:13 PM, Kevin Downey wrote: > > (f (first items)) => nil > ((f (fir

Re: Baffled by NPE

2009-11-03 Thread Kevin Downey
(f (first items)) => nil ((f (first items)) (for-each f (rest items))) => (nil (for-each f (rest items))) => (.invoke nil (for-each f (rest items))) => calling a method on nil is a NPE lists are function applications On Tue, Nov 3, 2009 at 9:33 AM, Dean Wampler wrote: > I'm working the exercise

Re: Baffled by NPE

2009-11-03 Thread Richard Newman
> I thought "when" was mainly used for side-effects? "if" seems more > appropriate if you just want the return value. It's perfectly appropriate and idiomatic when you want the alternative return value to be nil, and you don't want to confuse matters by having a one-element `if`, or an `if` t