Re: Outputting a triangle in Prolog?
- From: Geoffrey Summerhayes <sumrnot@xxxxxxxxx>
- Date: 17 May 2007 12:02:11 -0700
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
.
- References:
- Outputting a triangle in Prolog?
- From: Peppy
- Outputting a triangle in Prolog?
- Prev by Date: Re: exercise in prolog
- Next by Date: Prolog and AION
- Previous by thread: Outputting a triangle in Prolog?
- Next by thread: exercise in prolog
- Index(es):
Relevant Pages
|
|