Are Clojure API Functions Like Acupuncture’s Ear Points?

One theory of Acupuncture is that the human ear mirrors Acupuncture points on the rest of the human body. http://www.drgrotte.com/EarAcupuncture.shtml . Now, Clojure’s MAX and other functions are not really symmetrical to ear Acupuncture. That is you cannot learn everything about Clojure by studying just the max or any other single function.

However, I did notice with the max function http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/max that a lot of Clojure learning is packed within.

(defn max
"Returns the greatest of the nums."
{:added "1.0"}
([x] x)
([x y] (if (> x y) x y))
([x y & more]
 (reduce max (max x y) more)))

My answer to keeping around local state, like for writing my own max function as an exercise were futile, because I was thinking empirically. In the source code for max, the x or y value resulting from the if statement is returned back to max. This is the part of functional programming that will require a 90-degree turn of my mind in much the same way C++ required many years ago.

You can test the max function with this:


(do (println (max 12 21 -3 44 -6 3 4 7 5 22 17 23 18 19 9 )))

However, I suggest first walking through the max function code. Depending on the number of values presented to max, you might be surprised to see that the [x] x interface used, when your sequence is of an odd length. At first I wondered why for lack of a better term an identity function definition was needed, but that seems to be the nature of functional programming.

By having function “polymorphism”, max allows you to specify one, two, or multiple values for consideration and without trying to store interim values `a la an empirical style.

I can’t recommend strongly enough just tracing through the last interface


([x y & more]
 (reduce max (max x y) more)))

because you will see how it all works. Basically, you walk through the values in pairs, and if one value is left, it is the maximum value.

Advertisements

Leave a comment

Filed under Clojure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s