Blog 16: New plans
For some reason, the task of blogging has totally escaped me the past couple of days. I’ve completed the Haskell chapters on recursion and lists, and I feel pretty accomplished! I’m currently learning about folding lists, which is not as pleasant. Here’s an exercise from the book:
The following fold has an error. Fix it and then test it in your REPL.
foldr const ‘a’ [1..5]
If we check the type of const
(a function that takes two arguments and returns the first), we get:
const :: a -> b -> a
Now, let’s check the type of foldr
:
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
Do you see the problem? The type of const
does not align with the type required by foldr
. The solution is fairly simple: apply flip
to const
:
foldr (flip const) ‘a’ [1..5]
The type of (flip const)
is:
(flip const) :: b -> c -> c
This is a solution that took me quite some time to figure out. To no avail, I was filling my notebook with scribbles and parentheses. The solution :t
was just waiting for my in my REPL 😄
Another thing that doesn’t work:
foldl (:) [] [1..3]
(The (:) is the cons function that prepends a head to a list)
I really had no idea why this didn’t work as I wrote it out. Here’s what I wrote:
(([] : 1) : 2) : 3
([1] : 2) : 3
([2, 1]) : 3
[3, 2, 1]
But, no! The REPL throws an error because the type of the (:) function is (:) :: a -> [a] -> [a]
. The first argument must be some object of type a, and the second must be a list that contains objects of type a, and the function returns a list that contains objects of type a. The arguments cannot be switched.
The topic of folding is very confusing for me, but checking the types of the functions in these exercises help me see the errors and how to fix them. I’ll take more time tomorrow to work through the chapter.
I also met with Peri today to discuss my progress and the plan for the coming month. One suggestion he made is to rehearse my Haskell learnings through blog posts or lightning talks, hence the small exercise example above. Once I understand foldr and foldl better, I plan to write a more in-depth blog post on the topic. This is just a way to deepen my understanding of the topic. I shouldn’t treat it like a university course and mechanically work through the book and exercises just to get it done. I should treat it as a topic that I want to learn and, more importantly, teach others about (of course this is not to say that I haven’t been enjoying Haskell - because I do! Suprisingly!) Writing blog posts about any of the things I am learning is a way to articulate my thoughts, and I think this is especially helpful for something like functional programming, which confused and angered me in a previous life 🍃
After this, I met with Ugurcan to talk about the Heartbeat project and my goals. The first goal he suggested is to create a simple “Hello World” application and deploy it, just to learn about docker and the dust platform. This was a great suggestion, because it is something that I need to understand before I can move on with this project.
I made many new plans today. I have also added 3 trainings/workshops to my TODO list thanks to Peri’s and Ugurcan’s suggestions.
So much to do! So much to see! So much to learn! 🤓