Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog : Simple question

Tags:

prolog

I want to add any strings user entered into a 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 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 :(

like image 251
abduls85 Avatar asked Feb 22 '26 22:02

abduls85


1 Answers

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.

like image 191
Vincent Ramdhanie Avatar answered Feb 26 '26 19:02

Vincent Ramdhanie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!