0

I have a function square which I would like to display a matrix like this:

XXX (newline) XXX (newline) XXX 

My result is false after first line is printed. Why? How are variables transmitted in Prolog and how are the lines executed(order)?

line(Rez,X) :- Rez>=1 , write(X) , line(Rez-1,X). square(N,X) :- Rez = N,line(Rez,X),nl,N>1,square(N-1,X). 

1 Answer 1

1

There are several issue with your code. Notably, you're writing recursive predicate definitions without base cases. Another issue is that Prolog is not a functional language. When you write e.g. line(Rez-1,X), Prolog is not going to evaluate Rez-1 as an arithmetic expression. For Prolog, Rez-1 is just a '-'/2 compound term, i.e. a term with name - and two arguments. You will need to write something like NextRez is Rez-1, line(NextRez,X). The is/2 standard built-in predicate evaluates its second argument (as an arithmetic expression) and unifies the resulting with the first argument.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.