how do you print stars n amount of times on multiple lines using prolog

懵懂的女人 提交于 2019-12-20 05:55: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.



来源:https://stackoverflow.com/questions/59268954/how-do-you-print-stars-n-amount-of-times-on-multiple-lines-using-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!