Its been a long time since I finished the Io section of my Seven Languages in Seven Weeks Journey. I realize that. Probably six months. That is doubleplus ungood. In my defence I must remind readers that I said at the outset that I might not do these on seven consecutive weeks. I’m just stretching the nonconsecutiveness as far as I can!
I started working my way through the Prolog chapter late at night on a ferry with a shit internet connection. Well, I was amazed it had a connection at all frankly. I realized how much I use the web to look up synax, and I wondered at how I learned BBC Basic, GW Basic and Turbo Pascal from books and experimenting when I was knee-high to a grasshopper. Well, it all started out gently enough, and there was a great example of why Prolog's declarative style was cool with map colouring.
I found plenty of good tutorials — lots from the academic community — out on the web later on, but during my ferry trip I was pretty much just on my laptop typing code. The main discovery that I made was that single and double quotes are not at all the same in Prolog, at least not in the GNU variant that I was testing in. That caused a certain amount of consternation on my part. Otherwise the excercises were easy enough. The other main difficulty I encountered was that calling the source files .pl meant that vim filled in my boilerplate per code, and messed the syntax highlighting up. I couldn't be bothered to fix that this time, though.
bookshelf.pl
Make a simple knowledge base. Represent some of your favourite books and authors. Find all books in your knowledge base written by one author.
Well, that seemed easy enough. I came up with this.
$ cat day_one/bookshelf.pl
author_of('mutual aid', kropotkin).
author_of('history of the french revolution', kropotkin).
author_of('what is property', proudhon).
author_of('critique of state socialism', bakunin).
I could now load her up and do the query in question.
['day_one/bookshelf'].
compiling day_one/bookshelf.pl for byte code...
day_one/bookshelf.pl compiled, 4 lines read - 871 bytes written, 13 ms
yes
| ?- author_of(What, kropotkin).
What = 'mutual aid' ? a
What = 'history of the french revolution'
no
But the reverse query was also doable. Like this.
| ?- author_of('mutual aid', Who).
Who = kropotkin
yes
| ?-
Pretty cool.
music.pl
Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music.
This seemed rather similar to the last excercise. Well, that meant a nice relaxing time. I ended up having a bit of a mental block of musicians though!
$ cat day_one/music.pl
musician_plays('jimi hendrix', guitar).
musician_plays('john williams', guitar).
musician_plays(animal, drums ).
musician_plays('miles davis', trumpet).
musician_genre('jimi hendrix', rock).
musician_genre('john williams', classical).
musician_genre(animal, rock).
musician_genre('miles davis', jazz).
Again, pretty basic, but can be queried in many ways.
| ?- musician_plays(Who, guitar).
Who = 'jimi hendrix' ? a
Who = 'john williams'
no
| ?- musician_plays('jimi hendrix', What).
What = guitar
yes
| ?- musician_genre('jimi hendrix', What).
What = rock
yes
| ?- musician_genre(Who, rock).
Who = 'jimi hendrix' ? a
Who = animal
no
| ?- musician_genre(animal, rock).
yes
| ?- musician_plays(animal, flute).
no
| ?-
Well that was it for day one. I watched the end of Enter the Dragon and crashed out after that! Tune in next time. Which should be tomorrow. But who knows?