areinmeyer.dev

Learning Haskell for fun

December 01, 2022

Haskell Learning

As a way to challenge me and stretch my programming skills, I’ve decided to embark on a Haskell learning journey. Haskell is often hailed as one of the best ways to learn Functional Programming, wherein a developer builds small pure functions and composes them together to form larger bodies of structure and meaning. It is very different from Object-Oriented Programming, where objects in code represent a concept like a blog post or a comment on a page. One of the reasons to learn Haskell for me is to become a better JavaScript programmer, which has been my main programming language for the last 7 years. Many of the concepts learned in Haskell like partial application, pure functions, lambdas, etc are very possible in JavaScript without any add-on libraries. If you include Typescript as part of the JavaScript ecosystem, Haskell’s strong typing can also be a concept that transfers over.

My journey will include two books, Get Programming with Haskell, (Get Haskell) a Manning publication by Will Kurt that’s been sitting on my bookshelf for a little over a year. Also, the site (and book), Learn you a Better Haskell, (Learn You) (https://learnyouahaskell.com/). So far I have switched back and forth between both books as they complement each other. Kurt’s book takes the approach to introducing a new concept in each “Lesson” and at the end includes exercises that the reader can try and find answers to in the Appendix. It has lessons grouped into larger sections that cover a theme (FP concepts, Types). Learn You is a slightly more irreverent style that doesn’t provide reader exercises but has the benefit of being free online to read. For the most part, the books introduce the concepts in the same order, with the notable difference that Will Kurt’s book introduces Higher-order functions in the first section of the book as part of a Functional programming introduction/overview, while Learn You saves HOF’s as a later topic. Learn You introduces Types much earlier, whereas Kurt’s book saves those for a whole section. Both books are excellent so far. I’ve read Section 1 of Kurt’s book (Functions, Lists, Lambdas, Recursion, Higher-Order Functions) and the first 6 chapters of Learn You (Functions, Lists, Types, and Typeclasses, Syntax [guards, pattern matching], Recursion, HOF’s). For some of the concepts, it’s helped to read two different authors explain Haskell, as the code tends to be terse, yet dense. I’m not quite sure I’m comfortable with the differences in “let”, “where” and pattern matching yet. I’m looking to December when I can explore these concepts with the Advent of Code.

The Language

So let’s talk now about the language itself. Haskell so far seems very dense. It’s not cryptic as was Perl when I learned its arcane syntax and special characters oh-so-many years ago. But the flexibility of the standard library when dealing with lists or lists of tuples makes many complex concepts easy to express simply. As the programming concepts expand in the latter chapters, I will be curious to see how a more complex application looks in Haskell, especially once modules are introduced. JavaScript can be written densely, but usually map and reduce functions in JavaScript tend to be more verbose, with or without Typescript. The lack of parenthesis also can be challenging to read at times to one used to every function or even complex “if” statements containing parenthesis to provide clarity to the reader. The power of strong typing and simple but powerful functions though is a very powerful construct that I’m eager to try out. Using recursion frequently and constructing functions to leverage partial application is definitely something that can be used in JavaScript. Encapsulating tiny ideas into singular functions that can then build off of each other can come in handy quite a bit, especially if you work within a NodeJS framework with middleware concepts that can act as tiny functions, or React, which seems inspired by Functional Programming with pure components and isolating state changes, and it’s encouragement of immutable objects.

Probably one of the biggest mental shifts is the idea of managing state in functional programming. Most developers are likely used to updating objects, values, and arrays by adding, deleting, or otherwise manipulating different variables throughout their code. Functional programming encourages the concept of immutability. That is, if a function or block of code accepts a parameter or references a variable, it doesn’t change that variable to some other value. That’s considered a side effect of the function and is not allowed. The goal of each function is to accept parameters and return a value, and if the function is called 20 times with the same parameters, 20 times it should return the same value, regardless of the time of day/month/year, the values of any other variables currently in scope, or the machine the code is running on. Haskell, and functional programming in general, believes in building upon small functions, chaining them together, and by structuring your code in simple functions, you have confidence in the code working every time. Get Haskell has a ‘capstone’ project where Kurt tries to re-create OO concepts that did not quite land for me. My sense was that the project was to explore how a typical OO developer might start thinking in Haskell terms, but the example involved creating new robots as they took damage and the number of robot states created over 3 rounds did not seem like a good display of what Haskell can do?

Conclusion

I hope to explore some more detailed parts of Haskell as I go. Partial Application, Pattern Matching, and Types deserve more space than I want to devote to one singular blog post. I have dipped my developer toes into FP over the years, but never fully committed. Learning Haskell is giving me a new sense of understanding FP and the benefits it could have on my design and structure of projects.


Allen Reinmeyer

Written by Allen Reinmeyer who lives and works in Perrysburg, OH You should follow him on Twitter