# evan_tech

02:18 pm, 30 Apr 05

### high level

I recently read an essay about how to read mathematics. Math, the author argued, is a specialized and highly condensed language, and when reading a paper with math you should expect to spend time on the formulas for they say a lot with little text.

They give an example:
Take the following theorem from Levi Ben Gershon's (13th century) manuscript Maaseh Hoshev (The Art of Calculation):
"When you add consecutive numbers starting with 1, and the number of numbers you add is odd, the result is equal to the product of the middle number among them times the last number." It is natural for modern day mathematicians to write this as:

2k+1
Σ i = (k+1) (2k+1)
i=1

A reader should take as much time unraveling the two-inch version as he would unraveling the two sentence version.

It occurred to me today that when I first encountered functional languages (and when I later helped teach a class that had a lot of functional languages) I (/the students) had analogous difficultly with reading the meaning out of these heavily nested S-expressions. It felt to me like they were write-only, that some programmer was taking pleasure in golfing their code down to the minimal lines.

But I gradually discovered that it is just a question of being comfortable with the abstractions. For a long time (even through college) I read sigmas as for loops because that's the way I'd understand them. But mathematicians write sigmas instead because eventually the concept of summation becomes fundamental and more verbosity would only get in the way of the meaning.

It took me a while to become comfortable writing loops as tail-recursive functions in O'Caml, but eventually it became second nature. I find in Haskell you're rarely using recursion at all because you're instead composing higher-order functions. It's like each new abstraction collapses a new pile of ideas.

Russell was laughing at me for using accumulate() in some C++ code at work, and I eventually rewrote it as a for loop because it's important to match the culture of the code you're writing. But it's weird to go back to
`for (int i = 0; i < n; ++i) dest[i] = src[i]+1;`
`[x+1 for x in l]`
`map (1+) l` .
•  I would use `count`, `fill`, `accumulate`, `min_element`, and `max_element` (iterator methods that don't use functions) when I used to compete in Topcoder because less typing meant faster submissions. Personally, I think it's more readable too.I also use `transform` to convert a string to upper or lower case (using toupper/tolower from cctype). Is there a better way to do that in C++? reply to this
•  My pure mathematics skills have always been a little lacking. I also have never got around to looking at proper functional programming, despite intending to on several occasions. I always read sigmas like for loops, for the same reason that you did. Fortunately I'm quite happy with higher-order functions because of the fact that most of my work is with scripting languages which use them extensively. I guess Perl, which I seem to spend most of my time writing, is a weird mix of the two. I'm quite happy to write `@dest = map {\$_+1} @src` (though I do tend to shy away from such things because I don't like copying arrays like that) but implementing a loop using recursion with tail calls doesn't sit well with my conceptual model at all. I guess this just shows its time for me to get with the program. reply to this
•  You're right in pointing out the everybody wants the right level of abstraction and that what's right changes as we pick things up. There are so many different levels of abstraction out there, though, it's like planning a cross-country trip in advance without really knowing the driving customs of the place you're going to. Except that you're not allowed to switch lanes: if you picked one that's either too fast or too slow your trip is unlikely to end successfully.My small experience with FP is that there's a region of "wow, how beautiful", and a region of "this is utterly incomprehensible", with very little in between; unlike English, where I can very often maneuver and use my wider skills to figure out difficult texts. So learning of English in English is smoother, and (say) Haskell is just conscious confusion under the hope of enlightenment. And this meta-skill of living with confusion is necessary, because confusion itself is not going away, it just replaces its object from time to time.The real reason for this comment is to say that in Perl6 you can do`@l »+« 1`of if you want to change it in-place,`@l »++` reply to this