Welcome! Please see the About page for a little more info on how this works.

0 votes
in Transducers by

Hi guys. New to clojure. First post here. I'll try to keep it short. I can't figure out why this function doesn't work.

(def countToFive
    (fn [acc x] (into acc [(+ x (last acc))]))
    [1 1 1 1]

As the name suggests its supposed to count to five.

Thanks in advance to anyone who can help explain.

2 Answers

+2 votes

"...why this function doesn't work." Short answer: it is not a function. def just binds a symbol to the result of the enclosed form.

If you want a function, you can change the first line to

(defn countToFive []
Oh my goodness. My problem is that I was doing (println (countToFive)) instead of just (println countToFive). I spent hours staring at that definition going THIS SHOULD BE WORKING over that stupid problem. Thanks guys.
For stuff like this, getting on the Clojurians Slack and asking in the #beginners channel would get you real-time answers and get you unstuck a lot more quickly than the "ask" forum. You can sign up here https://join.slack.com/t/clojurians/shared_invite/zt-fvf7u6mr-HtHPDF6G3vIV8WfkEXfBaw -- just enter your email address (ignore what it says about work!) and it'll send you a link to complete your signup.
+1 vote

This produces [1 2 3 4 5] -- what are you trying to produce?