On Tue, Feb 22, 2011 at 4:28 PM, Jonathan Mitchem <jmitc...@gmail.com> wrote:
> Basically, when I'm solving the problem, I'd think "average is sum of
> the items divided by the count".
>
> So...
> (defn average [coll]
>  (/ (sum coll) (count coll)))
>
> Then, "since count is defined, I just need to define sum".
>
> (defn sum [coll]
>  (reduce + coll))

Basically, go ahead and start writing it top-down, and at the point
where you realize you want a helper function sum, add the declaration
above average.

It ends up looking like this:

; In average, we will be using a helper function sum, which will be
defined afterwards.
(declare sum)

(defn average [coll]
 (/ (sum coll) (count coll)))

(defn sum [coll]
 (reduce + coll))

In principle, I tend to prefer languages that let me structure the
code in whatever way I find the most readable, and I chafe that
Clojure makes it easier to write bottom-up code than top-down code.

But in all honesty, I've found it's not that big a deal to put
declarations up front if you really want to write in a top-down style.

-- 
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

Reply via email to