开发者

Prolog : Simple question

I want to add any strings user entered into a li开发者_JAVA技巧st

run :- write('How many students you have: '),read(x),nl.
       enterNameOfStudents(x).

enterNameOfStudents(x) :- for(A, 1, x, 1),write('Please enter the names of students'),read(A),??????,nl,fail.

What do i put in the ?????? portion to ensure that whatever the user enter will go into a user-defined list which will be used for further processing later ? Please help. I have tried numerous stuff like append and other but it does not work :(


enterNameOfStudents(0, Names):-!.
enterNameOfStudents(X, [N|Rest]) :-    write('Enter a name: '), read(N), nl,
                   X1 is X - 1, enterNameOfStudents(X1, Rest).

run(Names) :- write('How many students you have: '),read(X),nl,
   enterNameOfStudents(X, Names).

You can construct the list recursively like this. You need to pass an argument to run so that you get back the full list at the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜