barbie's notes - PROLOG - Part 5

I/O in PROLOG

Until the last post, we saw how to build rules and query facts. But all we can see as “answer” until now are some ‘yes’ or ‘false’. Pretty boring hum… But as in other languages, PROLOG also offers I/O support: please meet the methods and ```write()```.

You need to agree with me now that PROLOG is waaayyy easier than in the most of the other languages that you learned before, right? ;) It’s important to notice that we call the rule “hello” without any arguments and just after that we can pass the input to the

123456789101112131415161718

> Note that after each PROLOG instruction you need a dot!What we know as "Strings" from other languages should be written in PROLOG always inside " ". Input written outside " " will be interpreted and you would have a strange output...## RecursionIf you tried to implement ```ancestor``` as I suggested in the last post, you probably got it already. But if not, I figured that _RECURSION_ is still a big deal and deserves at least a small discussion here.Thinking recursively is something that needs practice. I have a "technique" that helps me everytime that I am stucked with some problem that I want to solve recursively.* First I solve the problem for the baseline (as in Math, you can think you are computing the ```x = 0``` or ```x = 1``` cases). In our family example, you could write the _base rule_ as:`ancestral(X,Z):-parent(X,Z).`* After that, you define the _recursive rule_ (or the `x = n + 1` cases):```ancestral(X,Z):-parent(X,Y), ancestral(Y,Z).

With this we can now ask who are Sarah’s decendents!

For that, add these two lines of code in your family.pl file, start your swipl and type: