Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Input , How can we do it?

Tags:

prolog

How can we get something from user in prolog : for example :

animal(dog).
animal(cat).
write('please type animal name:'),nl.
/* How to read from user and store it to X 
and then check that user has typed animal name ?*/
?-animal(X).
like image 229
S.A.Parkhid Avatar asked Feb 24 '11 16:02

S.A.Parkhid


People also ask

What is an example of user input?

Input device examplesMicrophone (audio input or voice input) Webcam. Touchpad. Touch screen.

Which function is used for user input?

In C programming, scanf() is one of the commonly used function to take input from the user.


1 Answers

You can use read for that. For example you could write read(X), animal(X). into the prolog interpreter or write this into a script file:

:- read(X), animal(X).

If you then enter a valid animal name into the prompt, it will be bound to X. If you enter an invalid name, it won't.

Or you could define a procedure like this:

read_animal(X) :-
  write('please type animal name:'),
  nl,
  read(X),
  animal(X).

And then call it in the in the interpreter like read_animal(X)..

Note that the input needs to be terminated by a ..

like image 169
sepp2k Avatar answered Nov 03 '22 16:11

sepp2k