Need help with this Prolog question.. 1. Given facts such as Bob is taller than Mike. Mike is taller than Jim Jim is taller than George Write a recursive program that will determine that Bob's height is greater than George's.
First you want to list your facts as your knowledge base: ``` taller(bob, mike). taller(mike, jim). taller(jim, george). ``` Then you make taller a transitive predicate. ``` taller(X,Y) :- taller(X,Z), taller(Z,Y). ``` Finally you would query. ``` ?- taller(bob, george). ``` It's been a long time since I've done prolog, but this is my best guess. I don't feel like getting an interpreter and there any any easy ones online.
So what will likely happen when you make the query: 1. It won't find `taller(bob, george)` anywhere and will resort to using `taller(X,Y)` 2. `taller(X,Z)` will iterate through `taller(bob, Z)`, starting at `Z=mike`. 3. This will be plugged into `taller(Z, george)`, resulting in `taller(mike, george)`. 4. Since it isn't in the knowledge base, it will resolve this rule using 'taller(X, Y)` again. 5. It will keep recursing through, eventually hitting `taller(jim, george)`, which will result in true.
Here is how I think stuff would translate into a procedural language: ``` function taller(X, Y) { if (X == bob && Y = mike) return true; // taller(bob,mike) if (X == mike && Y = jim) return true; // taller(mike,jim) if (X == jim && Y = george) return true; // taller(jim, george) // taller(X,Y) :- for (Z in [mike]) { // taller(X,Z) return taller(Z, Y); // taller(Z,Y) this is where the recursion happens } } ``` Though, I am just not completely sure.
Thank you!
Join our real-time social learning platform and learn together with your friends!