Print any line input using Prolog

Check that you can reliably format the line with the prologue of the base model (without modules). I have the following code that I thought would work, but not work:

hello(_) :-
  write('\nEnter Name: '),
  read(Name),
  format('\nThe name you entered is: ~s', [Name]).

This throws an error if the user entered Bob., but works if the user entered "Bob".or'Bob'.

ERROR: format/2: Illegal argument to format sequence ~s: _G7118

I also tried replacing the format writeqas follows:

hello(_) :-
  write('\nEnter Name: '),
  read(Name),
  writeq(Name).

Which does not produce an error, but simply finishes printing _L143insteadBob

+4
source share
2 answers

(Too long for comment)

read, , @lurker, "" ​​ "" . , : "?"

Prolog - . C , .

, hello/1 :

hello(Name) :-
    format("~w~n", [Name]).

hello("Bob") hello('Bob') , .

Prolog, .

+2

, read/1 Prolog. Prolog, :

read_line(Cs) :-
    get_code(C),
    ( C == 0'\n -> Cs = []     % end of line
    ; C == -1   -> Cs = []     % end of input
    ; Cs = [C|Cs1], read_line(Cs1)
    ).

SWI-Prolog, /2 SWI-Prolog ( ),

read_line(String) :-
    current_input(Input),
    read_string(Input, "\n", "\r", End, String).

, .

-1

All Articles