Teodor Pivnicki
Teodor Pivnicki

Reputation: 9

Make triangle of numbers in prolog

I have to do some predicate in prolog. My task is to make this triangle in imsert:

triangle(3):-

this is how it should look like this:

1

Upvotes: 0

Views: 875

Answers (1)

izaak_pyzaak
izaak_pyzaak

Reputation: 960

Here:

triangle(0).                               
triangle(N):-                              
N0 is N-1, triangle(N0), writeHalves(N). 

writeHalves(N):-                           
writeL(1, N), Ns is N-1, writeR(Ns, Ns). 

writeR(0, N):- write('\n').                
writeR(N, N):-                             
write(N), N0 is N-1, writeR(N0, N0).     

writeL(X, N):-                             
write(X), X0 is X+1, X\=N, writeL(X0, N).
writeL(N, N).                              

... although, i will leave working out the spaces up to you, as this smells like homework ;) one tip: Make a triangle/2 which must be proven to call triangle/1 which takes an accumulator to count the number of spaces needed, as the other term of the predicate. i.e triangle(N):- triangle(N, Accum) where Accum equals 0 and increments.

Upvotes: 1

Related Questions