I have been wanting to learn some functional programming and what better language to start with the original, at least I think it is the original. It is a pure functional language so I guess will force me to think differently than what I’m used to.
I am using the first AdventOfCode to learn it, I will go through the exercises, and let chatgpt help me learn the language. First I wanted to know how to read a file. This is what I got
main :: IO ()
main = do
contents <- readFile "input.txt"
putStrLn contents
My understanding of this is we are defining a main function that is required for all Haskell programs and serves as the entry point. On the next line do is meant to represent a sequence of steps, which sounds like imperative programming so not sure if this is the way I am meant to start a Haskell program.
In the next lines we are reading the file contents into a variable then printing it to the console. I guess the variable is immutable in Haskell? So is it still called a variable?
My first AdventOfCode challenge is to read a series of ‘(‘ and ‘)’ that indicate if I should go up or down a floor in a tower block. Currently we have a list of Char and need to check each and assign an integer value. I create a pattern matching function to do this which reminds me of a switch statement.
floorDirection :: Char -> Int
floorDirection '(' = 1
floorDirection ')' = -1
floorDirection _ = 0
I can then use the map function that transforms each element of a list, in this case it maps the ‘(‘ and ‘)’ chars to integers.
let floors = map floorDirection contents
The next function I learned is foldl which takes a list, an accumulator function and an initial value. The idea is that beginning with the initial value, foldl will iterate through the list and perform the accumulator function on the previous value. Here is the final code
main :: IO ()
floorDirection :: Char -> Int
floorDirection '(' = 1
floorDirection ')' = -1
floorDirection _ = 0
main = do
contents <- readFile "input.txt"
let floors = map floorDirection contents
let result = foldl (+) 0 floors
print result
So my accumulator function (+) simply adds each each element, so foldr will essentially keep a running total and add each value in the list to this total. In our case we have converted each char to -1 or 1 so we will move up and down the floors and find out where we end up!
PS C:\Users\ryanm\Documents\Haskell\AdventOfCode\2015\part1> ghc .\sol1.hs
[1 of 2] Compiling Main ( sol1.hs, sol1.o )
sol1.hs:9:1: warning: [-Wtabs]
Tab character found here, and in three further locations.
Suggested fix: Please use spaces instead.
|
9 | contents <- readFile "input.txt"
| ^^^^^^^^
[2 of 2] Linking sol1.exe
PS C:\Users\ryanm\Documents\Haskell\AdventOfCode\2015\part1> .\sol1.exe
280
A warning I guess I can revisit, but it spits out the correct result anyway! Another minor thing I learned is that the do statement essentially is to help make the code look more imperative for readability, and when you are in do-block you need to use let for variables or it doesn’t compile

Leave a Reply