how do you print stars n amount of times on multip

2020-05-03 05:57发布

问题:

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.



标签: prolog