I started to learn Prolog and I can't solve a difficult task for me.
On Halloween, three friends - Bob, Mark, Alex, chose the costumes of three ghosts: a ghost, a zombie, a werewolf. It is known that:
- Bob is the tallest.
- The one who chose the werewolf costume is shorter than the one who chose the ghost costume.
- Alex didn't fit the werewolf costume.
- None of the friends have the same name as the Halloween character from the selected costumes.
Which costume did each of the friends choose?
I solved the problem mathematically, but it is impossible to describe its solution in Prolog.
(the previous versions of this answer suffered from the "premature implementation syndrome". Here's another take on it)
Nah! :) If we can say it in English we can say it in Prolog:
So we were able to describe the problem in Prolog, after all.
Having described the problem properly, we already have our program to find the solution.
Testing:
This can be written a tiny bit shorter if we realize that selecting
bobas the tallestleaves us with
markandalexas the shorter ones:Glancing at this short code for a minute, we realize that it can be further simplified to
and this to
and this to
and this to
And this version we don't even need to run.