prolog

assert fact into file in prolog

Hello everyone, How can I assert a fact into a file without deleting the previous fact? In the following line, when I execute it twice, the second fact overwrites the first fact: tell('animal.txt'),write(Animal),nl,told. But when I use assert or assertz it will do nothing. Help me please. Thank you :) ...

Prolog, fail and do not backtrack.

Is there any build-in predicate in SWI-Prolog that will always fail AND prevent machine from backtracking - it is stop the program from executing immediately (this is not what fail/0 does)? I could use cuts, but I don't like them. Doing something like !, fail is not a problem for me, but in order to accomplish what I want, I would hav...

Matching tuples in Prolog

Why does Prolog match (X, Xs) with a tuple containing more elements? An example: test2((X, Xs)) :- write(X), nl, test2(Xs). test2((X)) :- write(X), nl. test :- read(W), ...

Reversible numerical calculations in Prolog

While reading SICP I came across logic programming chapter 4.4. Then I started looking into the Prolog programming language and tried to understand some simple assignments in Prolog. I found that Prolog seems to have troubles with numerical calculations. Here is the computation of a factorial in standard Prolog: f(0, 1). f(A, B) :- A ...

copy file into another file in prolog

Good morning/evening how can I write something in a file and then copy its content into the current file? for example I consult file1.pro then I have rule write something in file2.pro , after this rule finish its job I want append the content of the file2.pro int file1.pro . when I tried to append into file1.pro directly , the data a...

Prolog: find all numbers of unique digits that can be formed from a list of digits

The best thing I could come up with so far is this function: numberFromList([X], X) :- digit(X), !. numberFromList(List, N) :- member(X, List), delete(List, X, LX), numberFromList(LX, NX), N is NX * 10 + X. where digit/1 is a function verifying if an atom is a decimal digit. The numberFromLis...

Appending facts into an existing prolog file.

Hi, I'm having trouble inserting facts into an existing prolog file, without overwriting the original contents. Suppose I have a file test.pl: :- dynamic born/2. born(john,london). born(tim,manchester). If I load this in prolog, and I assert more facts: | ?- assert(born(laura,kent)). yes I'm aware I can save this by doing: |?...

prolog. recursive function returning multiple values.

I am new in Prolog. I need to declare a function which looks at a list like [[1, 0, 0], [1, 0, 0], [1, 0, 0]] and if the value is 0 returns its address(by considering it as a double array). I wrote a function basicly in the format: function(..., X) :- function(called by other values). How can I write a function, that returns a value...

Learn Prolog Now! exercise solutions

Does anyone know where I can find solutions to the exercises and practical sessions to Learn Prolog Now? I started working through it and would to verify the answers that I devise. ...

What is more interesting or powerful: Curry/Mercury/Lambda-Prolog/your suggestion.

Hi! I would like to ask you about what formal system could be more interesting to implement from scratch/reverse engineer. I've looked through some existing and rather open (open in the sense of free/open-source) projects of logical/declarative programming systems. I've decided to make up something similar in my free time, or at least ...

What does [a|b|c] evaluate to in SWI-Prolog?

The pipe operator in prolog returns one or more atomic Heads and a Tail list. ?- [a,b,c] = [a,b|[c]]. true. Nesting multiple pipes in a single match can be done similar to this: ?- [a,b,c] = [a|[b|[c]]]. true. What does the statement [a|b|c] infer about a, b and c? EDIT So far, all I can deduce is: ?- [a,b,c] = [a|b|c]. false....

Prolog homework problem?

the problem is ; we have a funtion take 3 argument, like; func ( [[0, 0, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]], (1, 1), X ) the first one is nested list, which is show 5x5 matrix and 1s means it is full, 0 means empty and, the second parameter (1,1) our starting point 1st row 1st column, the 3rd p...

How to solve this logical problem with Prolog?

That's my first question so please be tolerant. I've logical problem to write in prolog/CLP: "It is known only one character is telling the truth. Mr April says Mr May tells lies. Mr May says Mr June tells lies. Mr June says that both Mr April and Mr May tell lies. Write a program which determines who is telling the trut...

PROLOG - How to implement stack?

I have to implement stac in Prolog, but WITHOUT using list. Each element of stack should point to before element. Is there possible to do it? Could I define rules in runtime program? ( like: element('foo','bar'). where foo is content of element end bar is pointer to another? ...

Prolog lists and parameters and return values

Hi everyone, I'm new to prolog and I just can't figure it out. I'm trying to build a simple program that receives a list of predicates, searchs for a specific predicate in the list, and applies a function to that predicate parameters. something along these lines: ?- program([pred1(a,b,p), pred2(d,b,p), pred2 (a,c,p)]). program (lis...

Prolog Beginner: How to unify with arithmentic comparison operators or how to get a set var to range of values

I am new to prolog. I need to write an integer adder that will add numbers between 0-9 to other numbers 0-9 and produce a solution 0-18. This is what I want to do: % sudo code add(in1, in2, out) :- in1 < 10, in2 < 10, out < 18. I would like to be able to call it like this: To Check if it is a valid addition: ?- add(1,...

Prolog Beginner: Trivial Example that I cannot get to work.

I have some prolog. The lessThanTen and example predicates work as expected however the exam predicate does not work. lessThanTen(9). lessThanTen(8). lessThanTen(7). lessThanTen(6). lessThanTen(5). lessThanTen(4). lessThanTen(3). lessThanTen(2). lessThanTen(1). lessThanTen(0). % This should always return 5. example(X) :- X i...

Prolog: How to make three lists the same lenght (by adding leading zeros)

I am writing a prolog program to solve a problem. The problem takes three lists as input: solve( [L|Lr] , [R|Rr] , [S|Sr] ) :- Unfortunately the lists all need to be equal length for the program to work. So these work: ?- solve( [A, B, C, D] , [1, 3, 5, 6], [E, F, G, H]). ?- solve( [1] , [2], [3]). But these do not: ?- s...

Prolog Beginner: How to make unique values for each Variable in a predicate.

I have a prolog predicate: Add( [A|B] , Answer ) :- ... ~ Add everything in the list to come up with answer ... I would now like to implement AddUnique that would return unique values for everything in the list except when I give it the variable twice. Here are somethings that are logically equivalent: ?- AddUnique...

Prolog adding and removing list element if non present in second list

I don't know what I'm missing here. I want to add an element if it is in arg1 but not in arg2 and want to remove an element if it is in arg1 but not in arg2. I'm using an if condition with includes a function that return true if the element is in the arg2 list, false otherwise. Then use built in predicates append and select to add or r...