Question

I need the program that can be recognize the largest value in text. First streaming a text file in it and

I get the infos char by char but cannot do the calculation and result wheter it is square or not.. If it is square it give the number in sample.txt's coordinates.

    setup:-process('sample.txt').
process(File) :-
        open(File, read, In),
        get_char(In, Char1),
        process_stream(Char1, In),
        close(In).

process_stream(end_of_file, _) :- !.
process_stream(Char, In) :-
        get_char(In, Char2),
        look(Char,Char2), 
        process_stream(Char2, In).
Was it helpful?

Solution

In Prolog the easier to use input analysis tool is an extension called DCG (Definite Clause Grammar). It's available in almost any system, thanks to its simplicity.

Using that facility, the builtin read_line_to_codes and the utility integer//1 we can write:

:- [library(readutil),
    library(http/dcg_basics)].

setup :- process('sample.txt').

process(File) :-
    open(File, read, Stream),
    repeat,
    read_line_to_codes(Stream, Codes),
    (   Codes == end_of_file
    ->  close(Stream), !
    ;   phrase(check_rectangle, Codes, _), fail
    ).

% just print when |X2-X1|=|Y2-Y1|, fails on each other record
check_rectangle -->
    integer(X1), ",", integer(X2), ",",
    integer(Y1), ",", integer(Y2),
    {   abs(X2-X1) =:= abs(Y2-Y1)
        ->  writeln(found(X1,Y1,X2,Y2))
    }.

The output (I've added a scaled up rectangle just to test):

?- setup.
found(10,30,20,40)
found(100,300,200,400)
true.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top