For Programmers: Free Programming Magazines  


Home > Archive > Prolog > April 2004 > basic I/O









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author basic I/O
Lura

2004-04-07, 2:34 pm

Hello!
I start to learn I/O in prolog.
I have SWI prolog compiler.
I try the following program to get number from user and output cube:
cube :-
write('next item: '),
read(X),
process(X).

process(stop) :- !.

process(N) :-
C is N * N * N,
write('cube of '),write(N),write(' is '),
write(C),nl,
cube.

but when i try it i can't accept result. every time i'am in first 'read'.
?- cube.
next item: 2
|: 3
|: 3

thanks
bart demoen

2004-04-07, 3:33 pm

Lura wrote:
> Hello!
> I start to learn I/O in prolog.
> I have SWI prolog compiler.
> I try the following program to get number from user and output cube:
> cube :-
> write('next item: '),
> read(X),
> process(X).
>
> process(stop) :- !.
>
> process(N) :-
> C is N * N * N,
> write('cube of '),write(N),write(' is '),
> write(C),nl,
> cube.
>
> but when i try it i can't accept result. every time i'am in first 'read'.
> ?- cube.
> next item: 2
> |: 3
> |: 3
>
> thanks



read/1 reads a term which is followed by a dot . and a character like a
newline, a space, a tab ...
The newline might be necessary in any case, depending on whether you are
reading buffered or raw.
So, in response to next item:
type

2.<CR>

Cheers

Bart Demoen

Matthew Purver

2004-04-07, 3:33 pm

Lura wrote:

> Hello!
> I start to learn I/O in prolog.
> I have SWI prolog compiler.
> I try the following program to get number from user and output cube:
> cube :-
> write('next item: '),
> read(X),
> process(X).
>
> process(stop) :- !.
>
> process(N) :-
> C is N * N * N,
> write('cube of '),write(N),write(' is '),
> write(C),nl,
> cube.
>
> but when i try it i can't accept result. every time i'am in first 'read'.
> ?- cube.
> next item: 2
> |: 3
> |: 3
>
> thanks


read/1 expects a Prolog term as input, with a '.' character at the end. Try
'2.' as input, rather than just '2'

--
Matthew Purver - matt at purver dot org
Lura

2004-04-07, 5:34 pm

thanks a lot

Lura

2004-04-07, 5:34 pm

thanks a lot

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com