I recently picked up the Seven Languages in Seven Weeks book again, with the intention to start where I left off with Prolog, around the end of Day 1. As I was doing research for the exercises, I came across a great description that summed up exactly how I felt about this chapter:
“Today, Prolog broke my brain. The chapter started with recursion, lists, tuples, and pattern matching, all of which were tolerable if you’ve had prior exposure to functional programming. However, after that, we moved onto using unification as the primary construct for problem solving, and the gears in my head began to grind.”
At first, it seemed fairly easy to follow, very different to anything I’d done before, but that’s why I started with the Seven Languages book, to learn about new and different techniques in programming.
Reading through the day two section about lists and recursion, I started to find myself getting lost in the examples and it took a long time to understand what was going on. I couldn’t complete the day 2 exercises without a little help from the interwebz, although by the time I finished working through them, I did understand what I was going on. Trying to switch my brain from thinking about solving the problems in terms of rules rather than algorithms continued to bite me throughout the chapter though.
Some of the things I learned through Day 2 are pretty basic, but for a complete newbie to Prolog, they weren’t obvious.
In the factorial exercise, I realised that within a rule, I could add a line to validate the parameters – in this case that X > 0. Super obvious maybe, but Day 1 was all about matching rules and so this was new.
The next thing I learned was that you can have two versions of a rule with different conditions. I was already creating multiple versions of a rule to unify specific values such as 0 for factorial, but this was a different way to think about it.
As I worked through the sudoku and queens exercises, I still found myself wanting to do something like this:
Diags1 = [R1+C1, R2+C2, R3+C3, R4+C4, R5+C5, R6+C6, R7+C7, R8+C8],
… which just doesn’t work!
I did get there in the end with the queens solution, with a little help from the book to point me in the right direction for the diagonals.
In conclusion, I definitely learned a lot from this chapter, but struggled a lot as well! It was worth fighting through to the end though, as the concepts did start to make sense!