i need to print stars '*' n amount of time
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Very simple solution:
star(0).
star(N):-
N > 0,
foreach(between(1,N,_),write('*')),nl,
N1 is N-1,
star(N1).
?- star(3).
***
**
*
true
false
One of the problem in your code is that if you call star
with N-1
, at the next iteration N
will be unified with N-1
(as you write it, it does not performs the arithmetic operation). Instead you should do N1 is N-1
and then call star with N-1
. Then there are other problems... Look at my code to have an idea.