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.
精彩评论