On Tue, Nov 3, 2009 at 4:21 PM, Dean Wampler <deanwamp...@gmail.com> 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 4 5)) > > On Tue, Nov 3, 2009 at 3:13 PM, Kevin Downey <redc...@gmail.com> wrote: > >> >> (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 <deanwamp...@gmail.com> >> wrote: >> > 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 >> item. >> > Here's what I tried. >> > (defn for-each [f items] >> > (if (not (empty? items)) >> > ((f (first items)) (for-each f (rest items))))) ; line #3 >> > >> > (for-each (fn [x] (println (* x x))) (list 1 2 3 4 5)) >> > Here's the output: >> > 1 >> > 4 >> > 9 >> > 16 >> > 25 >> > Exception in thread "main" java.lang.NullPointerException (ex2.23.clj:0) >> > at clojure.lang.Compiler.eval(Compiler.java:4620) >> > at clojure.lang.Compiler.load(Compiler.java:4934) >> > at clojure.lang.Compiler.loadFile(Compiler.java:4901) >> > at clojure.main$load_script__7261.invoke(main.clj:210) >> > at clojure.main$script_opt__7298.invoke(main.clj:262) >> > at clojure.main$main__7322.doInvoke(main.clj:337) >> > at clojure.lang.RestFn.invoke(RestFn.java:413) >> > at clojure.lang.Var.invoke(Var.java:359) >> > at clojure.lang.AFn.applyToHelper(AFn.java:173) >> > at clojure.lang.Var.applyTo(Var.java:476) >> > at clojure.main.main(main.java:37) >> > Caused by: java.lang.NullPointerException >> > at user$for_each__1.invoke(ex2.23.clj:3) >> > at user$for_each__1.invoke(ex2.23.clj:3) >> > at user$for_each__1.invoke(ex2.23.clj:3) >> > at user$for_each__1.invoke(ex2.23.clj:3) >> > at user$for_each__1.invoke(ex2.23.clj:3) >> > at user$eval__4.invoke(ex2.23.clj:5) >> > at clojure.lang.Compiler.eval(Compiler.java:4604) >> > ... 10 more >> > >> > I tried replacing the list in the last line with a vector. Same result. >> > Suggestions welcome! >> > dean >> > >> > -- >> > Dean Wampler >> > coauthor of "Programming Scala" (O'Reilly) >> > - http://programmingscala.com >> > >> > twitter: @deanwampler, @chicagoscala >> > Chicago-Area Scala Enthusiasts (CASE): >> > - http://groups.google.com/group/chicagoscala >> > - http://www.meetup.com/chicagoscala/ (Meetings) >> > http://www.linkedin.com/in/deanwampler >> > http://www.polyglotprogramming.com >> > http://aquarium.rubyforge.org >> > http://www.contract4j.org >> > >> > > >> > >> >> >> >> -- >> And what is good, Phaedrus, >> And what is not good— >> Need we ask anyone to tell us these things? >> >> >> > > > -- > Dean Wampler > coauthor of "Programming Scala" (O'Reilly) > - http://programmingscala.com > > twitter: @deanwampler, @chicagoscala > Chicago-Area Scala Enthusiasts (CASE): > - http://groups.google.com/group/chicagoscala > - http://www.meetup.com/chicagoscala/ (Meetings) > http://www.linkedin.com/in/deanwampler > http://www.polyglotprogramming.com > http://aquarium.rubyforge.org > http://www.contract4j.org > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---