Article View: comp.lang.prolog
Article #3596Re: more on permutations
From: Richard A. O'Keefe
Date: Mon, 22 Aug 1988 22:22
Date: Mon, 22 Aug 1988 22:22
56 lines
2684 bytes
2684 bytes
In article <6880@well.UUCP> jax@well.UUCP (Jack J. Woehr) writes:
> What I am trying to do is accept user input, that, unlike
>ALL the examples in common texts on prolog IS NOT all in lower
>case, one word, period delimited. I want to get strings, wrap them
>in whatever they have to be wrapped in, and store them as terms.
>Then I want to write these predicates to a file, in forms like:
>
> customer('Joe Blow',bought('seven bananas and a Tricycle'),
> owes(13040),rated('a nuisance to Customer Service').
>
Does Clocksin&Mellish count as a "common text on Prolog"?
See section 5.2 "Reading and Writing Characters" and section 5.3
"Reading English Sentences" (I have the 3rd edition).
If you have Quintus Prolog and want to read a list of character codes
from the terminal after printing a suitable prompt, you just do
| ?- ensure_loaded(library(print_chars)).
| ?- ensure_loaded(library(prompt)).
| ?- prompted_line('Say something: ', X).
Say something: Some Thing!
Gloss: ^^^prompt^^^^^^***reply***
X = "Some Thing!"
More simply, to read a line from the current input stream as a list of
character codes, call
get_line(Chars)
where
get_line(Chars) :-
get0(Char),
get_rest_of_line(Char, Line),
Chars = Line. % postponed to make it steadfast
get_rest_of_line(Char, Line) :-
( Char =:= 10 /* newline character */ -> Line = []
; Line = [Char|Rest],
get0(Next),
get_rest_of_line(Next, Rest)
).
You can then split the line up as you wish (grammar rules are easily the
clearest way of doing that). The new-line character is system-dependent;
it was 31 in DEC-10 Prolog, and is 13 in some systems.
As for prompts, the built-in predicate prompt(OldPrompt,NewPrompt) sets
the prompt for character input, or you might just write(ThePrompt). E.g.
in Quintus Prolog
| ?- write('Who is John Galt? '),
get_line(Answer).
Who is John Galt? never heard of him.
Answer = "never heard of him."
>JAX on GEnie "I'll teach you Forth if you'll teach me Prolog!"
No thanks, I already know it. (One of the papers at the
Seattle conference was about defining Forth using Prolog.)
Technical Details
Message-ID:
<304@quintus.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!sundc!pitstop!sun!quintus!okReferences:
<1161@tuhold> <6850@well.UUCP> <298@quintus.UUCP> <6880@well.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: more on permutations Keywords: forth, prolog, binary, files, input, avocados Message-ID: <304@quintus.UUCP> Date: 22 Aug 88 22:22:19 GMT References: <1161@tuhold> <6850@well.UUCP> <298@quintus.UUCP> <6880@well.UUCP> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 56