
Functional programming often gets a bad rap for being confusing or just a bunch of nested functions, but it’s really about some core ideas that can make your code cleaner and safer—no matter what language you use. In this post, I’m diving into a key Haskell concept that’s super helpful for grasping these principles. The cool thing is, these ideas aren’t just for Haskell or FP fanatics; they can actually guide you to write better code even if you’re mostly into object-oriented programming. Trust me, it’s less mysterious than you think!
Code setup
In this post, I will use project setup leveraged nix flake to handle installing all packages you need without messing with system packages. I highly recommend using this repository to setup environment.
Functions the building block
Functions are the building blocks of most programming languages. I expect you have basic idea of function in main stream programming languages. There some characteristics that is particular to Haskell that for which I need to first present the simple example of
add
function.Int -> Int -> Int
add x y = x + y
The type suggests that the function takes 2
Int
s and returns another Int
. This might be look strange to you if you compare to typical input ->output
type. In most programming languages you would specify the inputs types as a tuple of types according to each parameter that function takes