Day 14: Errors, Errors, go away

My day started normally with a long hike to work, a daily, and a kata. Today, Wolfram joined the kata, but neither Masha nor Kay could join and Markus had to leave early.

Our current kata does not involve TDD (😱). The problem is called “Elevator Saga.” Each level provides a task to move x amount of people in some amount of time, using only a certain number of elevators. The simulator is the main source of testing, which provides random input to test your solution.

In the katas that I joined, we only made it to level 5. Our next step was to refactor to make it more readable and pass the simulation more often (πŸ˜…). Wolfram was grilling me with all these questions: “What is your goal?”, “How do you know when you have improved?”, “What is your approach?”, “What do you want to learn from this problem?”. I never thought about neither these questions nor their answers. My approach was just to pass each level.

So after explaining the half-baked solution to him, Wolfram suggested many ways to make the code more readable. He also taught me that creating an event listener inside of an event listener results in only creating the inner event listener when the outer even is triggered (and, it will do this every time). This is bad. My solution to this aspect was to create an event listener once for each floor object. There are many other ways in which the code needs to be improved. Right now when it is passing a floor on which people are waiting, the elevator does not pick them up. More on this tomorrow.

I also worked on Haskell today which was focused on recursion. I was getting some pretty headache-y errors in the REPL when trying to create a function that took an integer n and return a list that contains each digit, in order, of n.

Here is the error:

Non type-variable argument in the constraint: Num [a]
(Use FlexibleContexts to permit this)
  When checking the inferred type
    go :: forall a. (Integral a, Num [a]) => a -> [a] -> [a]
  In an equation for β€˜digits’:
    digits n
     = go n []
       go num list
       | num == 0 = list
       | otherwise = go (num `div` 10) ([num `rem` 10] + list)

It turns out I was creating my list incorrectly in the recursive call.
Instead of ([num `rem` 10] + list), I need ((num `rem` 10) : list). This was kind of hard to figure out because the error message is hard to interpret.

Time to end my workday normally: with a long hike home. Only this time, it’s raining.

Last updated: