dont know how to unificate this in prolog - prolog

Hi if i have the next knowladge base
natural(0).
natural(suc(X)):-natural(X).
sum(0,N,N).
sum(suc(N),suc(N),R).
mult(N,1,N).
mult(number,times,result):-mult(number,times-1,partial), sum(partial,number,result).
and i ask the query ? mult(5,3,15).
how does prolog runs this??? first i instanciate number to 5, times to 3, and 5 to result. And after it unificates with the head of the query, how does works with the body of the clause, specially what value would partial take

Related

Query() function in Google Sheets yields stray value: Why, and how can I fix it?

I would like to run a query statement that adds a bullet (●) to each row of output.
Here is my sample data:
Here is the query:
=query($A$1:$B$3,"SELECT '●',A,B",0)
Here is the result:
Why does the first stray line appear in the output ["●"()], and how can I write the query so that it yields only rows 2, 3, 4 and not the first row?
Note that the actual data is much more complicated than this, so solving it with a filter or array does not work well. I need a query solution. And I'd really like to know why that first line is appearing in the first place.
Thanks for the help.
KLS
=QUERY($A$1:$B$3, "SELECT '●', A, B LABEL '●' ''", 0)
This assigns an empty string to the label for that column, thus eliminating the label row.

Prolog - Why is member/2 not working here?

I don't know why this isn't working... here's the code.
cameToTheParty(date(15,9,2011), flor).
cameToTheParty(date(22,9,2011), marina).
cameToTheParty(date(15,9,2011), pablo).
cameToTheParty(date(22,9,2011), pablo).
cameToTheParty(date(15,9,2011), leo).
cameToTheParty(date(22,9,2011), flor).
cameToTheParty(date(15,9,2011), fer).
cameToTheParty(date(22,9,2011), mati).
cameToThePartyThatDay(Peoples, Date):-
bagof(X,cameToTheParty(Date,X),Peoples).
When I try
?- cameToThePartyThatDay(People,Day).
it says
People = [flor, pablo, leo, fer],
Day = date(15, 9, 2011) ;
People = [marina, pablo, flor, mati],
Day = date(22, 9, 2011).
But, when I try the following with a variable date's field, or an actual date, like...
member(X,cameToThePartyThatDay(People,date(15,9,2011))).
it just says
false.
The issue is that member is trying to find an element from the list cameToThePartyThatDay(People,date(15,9,2011)), which is not, in fact, a list.
What you want to do is:
cameToThePartyThatDay(People,date(15,9,2011)),
member(X,People).
... so that People is unified with the list of people who came to the party that day, and then member can pull elements from the list of People.
member(X,cameToThePartyThatDay(People,date(15,9,2011)))
is a wrong way to use member/2 because
cameToThePartyThatDay(People,date(15,9,2011))
isn't a list.
A right way could be
cameToThePartyThatDay(People, date(15, 9, 2011)),
member(X, People)
For Prolog, the boldface part in the following expression:
member(X,cameToThePartyThatDay(People,date(15,9,2011))).
Is not a call. In fact predicates are not functions: they do not return anything. According to Prolog the boldface part is a functor.
In order to let it work, you first call cameToThePartyThatDay and then you use People in the member/2 predicate, like:
cameToThePartyThatDay(People,date(15,9,2011)),
member(X,People).

Prolog return list of free days

I have a question (again :)). let assume we have the following data:
the first number is the day second the month and third the event
day(1,1, 'New Year')
day(1,1, 'The day after the new year')
day(23,1, 'The day i will finally understand this language :)')
day(14,2, 'Valentin's day')
day(16,2, 'Family day')
day(22,2, 'hein.. dont now lol')
nday(1,31).
nday(2,28).
nday(3,31).
nday(4,30).
nday(5,31).
I'm asked to create a predicate such that given a Day, it returns a month (only if the given day do not have event. for instance freeDay(23,X). X should have the value 2. if X equals 4, it should holds 1 (and if we enter the semi colon, it will return 2 since day 4 doesn't have any event in February as well. i have many more data. So i did the following but i get the value 0.
freeDay(_, Month,0):- Month > 12, !.
freeDay(X,Answer):- freeDay(X, 1, Answer).
freeDay(Day, Month,X):-
day(Day, Month,_),
W is Month + 1,
freeDay(Day, W, X).
freeDay(Day, Month, X):-
W is Month +1,
freeDay(Day,W, X).
Can you tell me what i did wrong please??
What your code does:
By calling freeDay(23,X), freeDay(X,Answer) succeds which calls the freeDay(X, 1, Answer).
Prolog first checks which of the predicates can be executed with the current inputs.
It finds out that freeDay(_, Month,0):- Month > 12, !. is not applicable since the month is 1 and it moves to the next predicate.
freeDay(Day, Month,X) is applicable, and what it does is increasing the month and calling itself(recursion).
The month keeps increasing when finally is more than 12. Remember everytime a predicate is called prolog checks the first predicate that it is applicable. Therefore this time freeDay(_, Month,0):- Month > 12, ! succeeds.
freeDay(_, Month,0):- Month > 12, !. breaks the loop (recursion) by the use of the cut annotation ! and returns the Answer which is 0 in this case. (In plain english here you say no matter what day is if month is greater than 12 return 0. This is the last statement executed.)
I hope you understand what's wrong. Generally logic programming requires thinking outside of the box. You need first to understand the problem and then attempt to solve it, in a simplistic way.

How can I select the second last item in a xpath query?

I'm new to xpath and I understand how to get a range of values in xpath:
/bookstore/book[position()>=2 and position()<=10]
but in my case, I need to get above 2 and one less then the total(so if there's 10 then I need 9, or if there's 5, I need up to the 4th spot). I'm applying my code to different pages and the number of entries is not always the same.
In python, I could do something like book[2:-2], but I'm unsure if I can do this within xpath.
You can use last() which represents the last item in the context:
/bookstore/book[position()>=2 and position() <= (last() - 1)]
In my case this was working for me to get last but one element
/bookstore/book[position() = (last() - 1)]

recursion in prolog - error in base case

I'm trying to write predicate range\3 that takes three parameters the first is the start, the second is the end and return the generated list in the third argument.
E.g rang(1,5,L).
L = [1, 2, 3, 4, 5]
I used this code
range(E,E,[E]).
range(S,E,L):-
S1 is S + 1,
range(S1,E,[S|L]).
But it does not work, when i used trace command to know where is the error i recognized that the base case is useless, I also tried the green cut !in the base case but it does not work range(E,E,[E]),!.
So, if any one knows what is the problem please help me
You're building the list in 'wrong' sense. Consider that when you'll call the base case, it will receive the consed list. How could match a single element list ? Try instead
range(S,E,[S|L]):-
S1 is S + 1,
range(S1,E,L).

Resources