On Lisp Chapter 5: Return Functions

Another On Lisp chapter has fallen this evening. I am fast approaching a quarter of the way through this book. I suspect my pace will change a bit once I hit the harder chapters.

Chapter Five tackles the fun task of returning functions.

Paul Graham first touches on the idea of orthogonality within programming languages. That is as a programming language designer you attempt to give the language a small number of concepts that can be used to express a wide number of programming concepts. With Lego blocks you can create nearly anything from a small number of actual block types.

To me, the number of programming languages that actually achieve this is fairly small, or perhaps it is the programming languages I try out. Maybe for a programming language to achieve this it has to be willing to be different which eventually precludes it from becoming popular. I haven’t thought deeply on this recently so perhaps this statement is not totally correct.

A modern programming language that seems to be fitting the bill of having a small number of concepts but also expressive is Zig. This positins itself as a competitor to C and hence by extension C++ and Rust. Zig has successfully used compile time evaluation to remove specific generics syntax along with C style macros from the language. I am no expert and to be fair there is a compile time Type that is used to return new types.

In C++ (and maybe Rust) compile time evaluation was just another addition, that may eventually grow to supercede language concepts in C++, it really depends on where it heads in the future. The concept of compile time programming was clearly common in Lisp around the invention of C++ and would have reduced the number of concepts that needed introducing.

The idea of a strongly orthogonal set of core language concepts is not new to me but it is something I feel programming languages should strive for.

And back to the actual chapter

You have probably seen the classic make counter examples and that is many tutorial about returning functions. Well it is here as well.

We quickly move on to the memoize function that lets you quickly start to use functions using the ideas from dynamic programming. While I have seen this code in other dynamically typed languages it is still pretty cool how little code this requires. Statically typed languages traditionally have a harder job at getting this code to look elegant.

Fairly soon after that we are extracting repeated patterns of recursion over trees.

I didn’t really notice until I came to write this but no new Lisp concepts where introduced. We were just using functions, labels and other bits of lisp in different ways. Pretty cool as we are only at the end of chapter 5.

I enjoyed this chapter, certainly more than chapter 4. It didn’t really teach me that much but I think it is because I have done a fair amount of recursion in the past, it was certainly a good refresher. If you are not comfortable with recursion then this chapter could prove difficult. I recommend playing with each example until you full understand it. Recursion is a great way to break down a problem.

Oh I forgot, mostly we were creating and returning functions quite a bit in the chapter but for me that fell into the background.


lisp

568 Words

2020-08-31 21:28 +0000