Re: Outputting a triangle in Prolog?



On May 16, 8:07 pm, Peppy <BrenOMah...@xxxxxxxxx> wrote:
I am trying to write a prolog predicate triangle which finds the
number of balls in a triangle of base N and then draws the triangle
using write('o') to draw a ball. (I'm using Win Prolog)

e.g. if you run...
?-triangle(4).

you should get...

6 balls
o
oo
ooo

I get the first part right but I can't get any more than one line of
balls to appear using my code...

triangle(X):-X>0,
C is X * (X + 1) / 2,
write(C),write(' balls'),nl,
drawlines(X,C),nl.

drawlines(X,C):- C>0,drawball(X),drawlines(X-1,C-X),nl. %draws N
balls and then N-1 balls etc.

drawball(X):- X>0,write('o'),drawball(X-1).

Your output is upside down from your example and the
newline in drawlines/1 is in the wrong place.

Hint questions:
-What does drawball/1 do when its argument is 0?
-How does that affect the statements drawlines/2
and triangle/1?

---
Geoff

.



Relevant Pages

  • Re: Integer tetrahedron
    ... You could make a coordinate system from the whole to the parts, ... and put a smaller triangle of balls on top of the big triangle of balls ... The four planes that defined the ...
    (sci.math)
  • Re: Weekly Video Clips from Greg Kennedy
    ... The problem was that I was connecting ... the balls in the wrong way. ... string triangle. ... My new video Clip, Week #5 - Triad is up at: ...
    (rec.juggling)
  • Re: Simple way of making Golf Ball with Solidworks!
    ... balls. ... sketch to project onto a surface. ... the vertexes or the face centerpoints lie on the sphere. ... split a 2D triangle up into four identical 2D triangles, ...
    (comp.cad.solidworks)
  • Re: Simple way of making Golf Ball with Solidworks!
    ... balls. ... sketch to project onto a surface. ... split a 2D triangle up into four identical 2D triangles, ... the dimples requires a little adjustment. ...
    (comp.cad.solidworks)
  • Outputting a triangle in Prolog?
    ... I am trying to write a prolog predicate triangle which finds the ... number of balls in a triangle of base N and then draws the triangle ...
    (comp.lang.prolog)