HN Books @HNBooksMonth

The best books of Hacker News.

Hacker News Comments on
From Mathematics to Generic Programming

Alexander Stepanov, Daniel Rose · 3 HN comments
HN Books has aggregated all Hacker News stories and comments that mention "From Mathematics to Generic Programming" by Alexander Stepanov, Daniel Rose.
View on Amazon [↗]
HN Books may receive an affiliate commission when you make purchases on sites after clicking through links on this page.
Amazon Summary
In this substantive yet accessible book, pioneering software designer Alexander Stepanov and his colleague Daniel Rose illuminate the principles of generic programming and the mathematical concept of abstraction on which it is based, helping you write code that is both simpler and more powerful. If you’re a reasonably proficient programmer who can think logically, you have all the background you’ll need. Stepanov and Rose introduce the relevant abstract algebra and number theory with exceptional clarity. They carefully explain the problems mathematicians first needed to solve, and then show how these mathematical solutions translate to generic programming and the creation of more effective and elegant code. To demonstrate the crucial role these mathematical principles play in many modern applications, the authors show how to use these results and generalized algorithms to implement a real-world public-key cryptosystem. As you read this book, you’ll master the thought processes necessary for effective programming and learn how to generalize narrowly conceived algorithms to widen their usefulness without losing efficiency. You’ll also gain deep insight into the value of mathematics to programming―insight that will prove invaluable no matter what programming languages and paradigms you use. You will learn about How to generalize a four thousand-year-old algorithm, demonstrating indispensable lessons about clarity and efficiency Ancient paradoxes, beautiful theorems, and the productive tension between continuous and discrete A simple algorithm for finding greatest common divisor (GCD) and modern abstractions that build on it Powerful mathematical approaches to abstraction How abstract algebra provides the idea at the heart of generic programming Axioms, proofs, theories, and models: using mathematical techniques to organize knowledge about your algorithms and data structures Surprising subtleties of simple programming tasks and what you can learn from them How practical implementations can exploit theoretical knowledge
HN Books Rankings

Hacker News Stories and Comments

All the comments and stories posted to Hacker News that reference this book.
It is not about distaste, though idea about keeping and carrying 1 around is a recipe for bugs. It is all about composability.

Could you make proper monoid out of Julia ranges? What would be a unit in that monoid? Could you tell me how empty range looks in Julia (in Python/C++ it looks trivial)?

> Python cannot even index lists by ranges (or other lists)

sure, Python has deficiencies as well.

C++ STL iterators/ranges are made in the same way, and for a good reason - composability first. It described very well in Alex Stepanov book (https://www.amazon.com/Mathematics-Generic-Programming-Alexa...), as well as how it helps when we move to parallel algorithms (http://stepanovpapers.com/p5-austern.pdf).

It is even more explicit in upcoming Ranges library in C++20

3JPLW
An empty range in Julia is `1:0`. So yes, they can form monoids just fine. I highly recommend you work with them _directly_ instead of carrying around the two endpoints separately. For example, you can divide any range into two parts with:

    split(r) = r[1:end÷2], r[end÷2+1:end]
That'll happily recurse and eventually spit out empty ranges given an input like `split(32:56)`.

A zealous adherence to one strategy or another will simply blind you to the ways in which the other might be easier at different times.

Yes! Alexander Stepanov is fantastic. He's the designer of the Standard Template Library (STL) for C++, which is C++'s core library of generic algorithms. It's part of the standard libary.

If you can understand how to use the STL properly, you're well on your way to becoming a profiicent C++ programmer.

His books are fantastic too.

Elements of Programming:

https://www.amazon.com/Elements-Programming-Alexander-Stepan...

and From Mathematics to Generic Programming:

https://www.amazon.com/Mathematics-Generic-Programming-Alexa...

If you prefer video lectures, his second book is based off his lecture series Four (three) Algorithmic Journeys:

https://www.youtube.com/playlist?list=PLHxtyCq_WDLV5N5zUCBCD...

https://www.youtube.com/playlist?list=PLHxtyCq_WDLW0NqZCcrrQ...

https://www.youtube.com/playlist?list=PLHxtyCq_WDLXrHwcaay14...

https://www.youtube.com/playlist?list=PLHxtyCq_WDLVQPzEm3igP...

It's a combination of history and math/algorithms and programming (using C++). If you're just looking for a straight intro to C++ course this isn't it. But it's a ton of fun and the generic programming/STL mindset is very powerful.

Programming Conversations is another great lecture series by Alexander Stepanov:

https://www.youtube.com/playlist?list=PLHxtyCq_WDLXFAEA-lYoR...

I would also recommend searching YouTube for videos by Sean Parent. This one in particular is very enlightening:

https://www.youtube.com/watch?v=qH6sSOr-yk8

Sean Parent is very good at getting you in the STL mindset and showing off the expressive power of using the standard STL algorithms in your code. Except in the simplest cases, you should try to use them instead of writing your own loops.

Here are some random tips if you're coming from C or Java:

"new" is not the way to create objects in C++. new and delete should almost never be used by serious programmers in C++. Never use new and delete (or malloc and free). If you want a dynamically-allocated array, use the standard vector.

It's much easier to write C++ than it is to read it. This is because you can always write using a simple clear subset of C++ that you understand. Try to pick a style that you think as many people as possible will understand. Programming languages are for humans to read. I try to write code that I think C programmers can read.

Edit -- Another tip:

Exception safety isn't about C++'s exception-handling language feature. Exceptions still happen in C. There's just not a language feature that directly expresses them.

Writing exception-safe code is nearly impossible in C. RAII and C++'s built-in exceptions make it possible. If you're careful to always use RAII by default, you can get the basic exception safety guarantee automatically without thinking about it. And you can get the strong exception safety guarantee whenever you need it.

Good luck!

keldaris
> It's much easier to write C++ than it is to read it. This is because you can always write using a simple clear subset of C++ that you understand. Try to pick a style that you think as many people as possible will understand. Programming languages are for humans to read. I try to write code that I think C programmers can read.

While I don't disagree with the sentiment (quite the contrary), if writing code that C programmers can easily read is the goal, using the STL is probably not the best idea.

Personally, while I'm very biased by my usecase for C++ (compute-bound performance sensitive numerical code), I quite enjoy writing code that's essentially C with function overloading, operator overloading and a few judiciously placed templates. I don't miss the STL, OOP, smart pointers and the rest, and the language suddenly seems much more concise and convenient, not to mention compile times are virtually instant and debug builds aren't unusably slow any more. YMMV, of course, everyone should pick the subset of C++ that suits their usecase and relevant constraints.

Every good programmer should know? At least abstract Algebra.

The thing about advanced maths is that it forces you to think about abstractions in ways that not even programming forces you to think. You can get pretty far programming without much abstraction, but you can't understand the mathematical fact that any group of order four is either cyclic or isomorphic to the Klein-four group[1] without thinking in a really abstract way. The more you study these abstractions in advanced maths, the better you get at thinking in an abstraction-first way.

A great book that just came out this year explains this really well by taking you from a basic algorithm to an abstract implementation, explaining the maths along the way (from multiplication to abstract algebra and number theory). It's titled From Mathematics to Generic Programming and I recommend it to anyone who wants to understand more of what I mean[2].

--

[1]: http://math.stackexchange.com/questions/165341/any-group-of-...

[2]: http://www.amazon.com/Mathematics-Generic-Programming-Alexan...

j2kun
There are many ways to learn about abstraction, and abstract algebra is not the only one. I.e., assorted facts about groups are mostly irrelevant, and if you can learn how to think abstractly by studying, say, graph theory, why would you need abstract algebra?
insoluble
I completely agree. Logic or philosophy can help one to practice complex abstraction. Even the study of law (as in legal code) or religion involves abstract thinking.
HN Books is an independent project and is not operated by Y Combinator or Amazon.com.
~ yaj@
;laksdfhjdhksalkfj more things
yahnd.com ~ Privacy Policy ~
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.