# devNoo

6 January 2012

## Seven languages in seven weeks: My solutions for prolog day 3

Finally I’m able to continue the seven languages in seven weeks book. I didn’t have time before because I became father for the second time. My son Thomas was born on the 24rd of december.

Before this I was still contemplating the day 3 exercises of the prolog chapter. I had the solution in about 10 minutes, because you can get a solution to the sudoku solver by copy pasting the code of the sudoku 4×4 solver and adapting it to solve 6×6 sudokus. But this is not quite satisfactory. First of all it is against the DRY-principle, I did not want to repeat my self 3 times to solve 3 different kind of sudokus. This is for me not the most important reason, because the DRY-principle is sometimes overrated and costs you in the clarity of the code. But more importantly, there is no challenge in the exercise if you just mindlessly copy paste the code to a 6 by 6 and 9 by 9 sudoku.

So I was looking for a more general sudoku solve method that could easily be used to solve NxN sudokus. This was much harder than expected. I got frustrated to the point that I quit several times, but at the same point prolog intrigued me and after a couple of days I wanted to take another try. One of the reasons I got frustrated a lot, were all the bugs in the gnu prolog 4.0 version I installed with homebrew. All arithmetic functions would give me a segmentation error, so even 1+1 came up with this error. After a while I decided to use ideone.com and this worked quite well.

Finally I came up with this solution. I’m sure it is not the best Prolog code you will find to solve the problem, but at least it gave me a better understanding of prolog.

7 December 2011

## Seven languages in seven weeks: My solutions for prolog day 2

This post will present my solutions for day 2 of the prolog chapter of the excelent book seven languages in seven weeks:

After the basic introduction of day 1, day 2 dives into unifications and recursions. It took me quite some time to get used to Prolog, especially the fact that you don’t return values from the rules, but pass in a param. Of course this makes sense if you look at prolog and logical-languages in general, because you want to create logical statements that are true or false. Then you can use the statement with a variable to see which value the variable have to be to make the statement true.

Anyways here are my solutions.

Reverse a list

nothing really special, I started with the empty list, then list with 1 member and continued, until it was clear how the regression part should be implemented.

Here are the run results on ideone: http://ideone.com/RZRvY/.

(BTW, if anyone knows how to combine gists and ideone run in wordpress it would be greatly appreciated).

tracing
in GnuProlog you can easily enable tracing with the command:
``` trace. ```
I really needed this for the next two exercises.

smallest
I came up with a solution for the smallest number in a list, but it it wasn’t satisfactory because Prolog is not sure there is one answer. It will give you the option to see al posibilities, but there is just one. Bit strange, so if someone who reads this can point out what causes this I will be very gratefull.

After some googling and I found this solution.

Restults at ideone.com
This one has almost the same problem if you pass in a list of 1,2 or 3 elements it works fine. But if you pass in 4 elements it will also ask you if you want more solutions, while there are none.

Update:
My colleague Mark van der Voort helped me find a real solution for the smallest:

The ! is used in prolog to indicate that it does not have to search for other solutions anymore.

I could not figure this one out on my own, and looked up some insert sort sample which I then adjusted to my taste.
here it is.

conclusion
I still like the power prolog gives you but had quite some problems implementing the exercises. I’m not sure if a solution is right when it asks you if you want to see more solutions for What. I doubt it anyway.
Next chapter seems interesting, letting the computer solve a sudoku.

3 December 2011

## Seven languages in seven weeks: My solutions for prolog day 1

I just finished Prolog day1, and really liked it. It is completely different then any languages I ever worked with. But what I really like is that you let the computer solve logical problems. As a kid, and still now, I was fond of solving Zebra Puzzles(logigram in Dutch), and prolog seems to be a perfect languages to solve this kind of puzzles.

Installing prolog on Os X Lion was a breeze, I used homebrew, and just ran

```brew install gnu-prolog
```

which installed gnu prolog 1.4.0.

While running the examples I ran into some problems. I got the error: syntax error: . or operator expected after expression when running the scripts that came with the book.

After some research I found the problem to be in the file header comments:

```#---
# Excerpted from "Seven Languages in Seven Weeks",
# .....
#---
```

is apparently no a valid comment statement for my version of gnu-prolog.

Anyway, here are my solutions:

1 December 2011

## Ubuntu 11.10 network shares to Os X lion with netatalk

I had quite some problems getting netatalk to run again after upgrading my server to ubuntu 11.10.
This will explain the way I did it(also as a note for myself when I need it again).

first I removed netatalk completly, you can make a backup of your /etc/netatalk if you want.

```sudo apt-get remove netatalk --purge
sudo rm /etc/netatalk -R -f
```

Now I added a new repository with netatalk 2.2.1 in it to the sources for apt.
edit /etc/apt/sources.list and add the following lines.

now reinstall netatalk

```sudo apt-get update
sudo apt-get install netatalk
```

now you have to edit the file /etc/netatalk/afpd.conf

Watch it, this is different form the most used one in ubuntu versions before 11.10, the uams_dhx2.so is replaced by uams_dhx2_passwd.so, cause of a bug in ubuntu netatalk, see the bug report at https://bugs.launchpad.net/ubuntu/+source/netatalk/+bug/810732

also I added the logging options to get some feedback if it goes wrong.

Now edit /etc/netatalk/AppleVolumes.default with your normal shares:

finaly restart netatalk:

```sudo service netatalk restart
```

This did the trick for me, let me no if it does not work, might be I forgot a step cause it took me hours to get the whole thing working again.

30 November 2011

## Seven languages in seven weeks: My solutions for Io day 3

I just finished Io day 3 of the excellent book Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers)

Here are my solutions, and a general wrap up of my toughs on Io.

Xml builder with indentation
Basically we have to modify a given xml builder to use indentation. I did not run in to a lot of problems. Only thing I don’t like in this solution is that you have to put state in to the object to keep track of your current indentation, instead of passing it as a param to the recursive calls. I did not succeed in using it as a param because of the meta-programming parts. But probably it will be possible.

27 November 2011

## Seven languages in seven weeks : My solutions for Io day 2

In day 2 of the Io chapter I got a much clearer view of what Io was, and how you could use it.
There are a bunch of exercises, here are my solutions.

Exercise 1

Fibonaci generator with recursive calls:

26 November 2011

## Seven languages in seven weeks: My solutions for Io day 1

Here are my solutions to Io day 1.

It will be a very small post, seen that there is only 1 simple exercise.

Basically you have to add a slot to an object and execute it.

This is my solution:

Io was a bit strange for me, first of all i constantly wanted to put a dot between the object and the message, but especially the whole idea of prototyping is quite new to me.

15 November 2011

## Seven languages in seven weeks My solutions for Ruby Day 3

In this post i will present my solutions to the selfstudy exercises of day 3 in the ruby chapter of the book: Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers)

In the third chapter the meta programming options ruby gives you are discussed. There is only one exercise, but it is a bit more challenging then the previous exercises.

Here is my solution:

12 November 2011

## Seven languages in seven weeks: My ruby solutions – Day 2

In this post i will discuss my solutions to the selfstudy exercises of day 2 in the ruby chapter of the excellent book: Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers)

So lets get started:
The exercises begin with some find exercises that basically let you search for ways to do certain stuff in ruby