Prologue: A Simple Question

I want to add any lines entered by the user to the list

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 ?????? to ensure that everything the user enters is included in the user list, which will be used for further processing later? Please help. I have tried many things like append and stuff, but this does not work :(

+3
source share
1 answer
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 build a recursive list this way. You need to pass an argument to run so that you return the full list at the end.

+1
source

Source: https://habr.com/ru/post/1736853/


All Articles