Introduction to Lisp: Heron's method
learnclojure.blogspot.com
learnclojure.blogspot.com
Yet there isn't a single macro or piece of code writing code implemented in this example. Also, it isn't magic, it is list construction. For the last time LISP IS NOT MAGIC.
"This is clearly seriously annoying, and like monads in Haskell, it's the first thing you hit when you learn lisp, but I promise that after a month or so of using it you stop noticing it, and although it's never quite as good for actual arithmetic, it's actually much nicer as a notation for a generalized function call."
No, it is seriously clearer than having implicit rules for order of operations that you have to memorize. Not annoying, and much better for arithmetic.
Also, there are a boatload of built in functions in clojure-contrib related to math. USE THEM. There is no need to waste time reimplementing absolute value when it is in clojure-contrib already.
But I'm writing something more devious just in case.
Any sufficiently advanced technology is indistinguishable from magic. I do know how it works. I've done my meta-circular interpreters.
I also know there's an abs function. There's a sqrt function too. That's not really the point of the thing.
And I am never going to believe that (+ (* x x) (* y y)) is clearer than xx + yy, even though I do realise that that's only because those rules were drummed into me at school.
More exotic examples of operator precedence, yes, I agree.
I understand that the sqrt function is done for teaching purposes, but there is no point in showing people how to write an abs function, all it does is give the impression that Clojure doesn't have an absolute value function.
It would be a better use of time to take the opportunity to show people how to import a function from an external library (like clojure.contrib), than it would be to re-implement a relatively mundane function. Using libraries is important and something that should be covered in an introduction to a language.
My point about operator precedence is that it is clearer because it is explicit. When you add exponentiation into infix math notation, it gets very easy to screw stuff up by missing a paren. xx+yy is ambiguous with x(x+y)y, in lisp it would be (+ (* x x) (* y y)) vs. (* x y (+ x y)). (The point being that they have vastly different structure).
(Is it ironic that I'm arguing that missing a paren is more annoying in infix than it is in prefix?)
Also, reimplementing functions that are already there is a perfectly acceptable way to learn a language.
Clarke's Law, "Any sufficiently advanced technology is indistinguishable from magic". If you are used to java, clojure - or any lisp - is sufficiently advanced to be considered magic.
Edit: possessive apostrophe
Fortunately for us, lisp is probably the exact opposite of a magic-like programming language. All you are doing is composing lists of symbols into trees of symbols and then executing them. It is stupidly simple.
Saying that lisp is magic serves the sole purpose of scaring newbies and spreading the fud that using lisp is a complex, complicated, and difficult. (it is not).