Now I Understand Why People Love Functional Programming
My first impressions of functional programming with SML
This year I wanted to revisit some undergraduate topics with a fresh perspective, ten years after starting university. The course I chose was Programming Languages Part A from the University of Washington. Part A covers topics in functional programming languages using StandardML.
StandardML (or SML) was the predecessor to many other well-known functional languages, such as OCaml. The language is strictly typed, but what really stood out to me was its powerful type inference system with polymorphic types.
Coming from the TypeScript world, when writing a simple function, you first need to properly type the parameters. Additionally, if the function can be applied flexibly to abstract data types while maintaining proper typing, you use generics.
function addFrontAndBack(arr: Array , elem: T): Array { return [elem, ...arr, elem]; }
Now, looking at the SML example, you don't need to manually type anything. Yet, the code remains type-safe and without the annoying generics.
fun add_front_and_back (arr, elem) = [elem] @ arr @ [elem]
Combining this with automatic garbage collection, exception handling, and other features, it feels like SML was ahead of its time (1983).
After completing this course, I started practicing more in my own code the separation between parts that have side effects and purely functional parts, something that is almost mandatory in functional languages, but is a good practice in any programming language. This separation makes the code easier to understand and to test.
Now I understand why so many of us love functional programming.