Home > Archive > Prolog > May 2007 > Reading terms
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]
|
|
| behdad 2007-05-03, 10:02 pm |
| Hi , I want to read list of facts from file X and upon a filteration
write them back to itself , kind of updating ,
the way that i try to implement it is something like this :
remove_fact(RF,WF,FACT) :-
see(RF),
open(WF,write,OS),
repeat,
read(X),
write(OS,X),write(OS,'.'),
X \== FACT,
X=end_of_file,
!, seen,told,close(OS).
as far as im new in prolog any tip , hint , help would be gold to me .
Thanks alot
| |
| Carlo Capelli 2007-05-04, 4:10 am |
| "behdad" <behdadkh@gmail.com> ha scritto nel messaggio
news:1178219751.187955.41930@o5g2000hsb.googlegroups.com...
> Hi , I want to read list of facts from file X and upon a filteration
> write them back to itself , kind of updating ,
> the way that i try to implement it is something like this :
>
> remove_fact(RF,WF,FACT) :-
> see(RF),
> open(WF,write,OS),
> repeat,
> read(X),
> write(OS,X),write(OS,'.'),
> X \== FACT,
> X=end_of_file,
> !, seen,told,close(OS).
>
> as far as im new in prolog any tip , hint , help would be gold to me .
> Thanks alot
>
You can try this code in SWI-Prolog: i don't known if it's ISO portable.
You must obviously write your own qualify/1.
Bye Carlo.
filter(PathIn, PathOut) :-
open(PathIn, read, FileIn),
open(PathOut, write, FileOut),
filter_stream(FileIn, FileOut),
close(FileOut),
close(FileIn).
filter_stream(FileIn, FileOut) :-
repeat,
read(FileIn, T),
( T = end_of_file -> true
;
( qualify(T) ->
writeq(FileOut, T),
write(FileOut, '.'),
nl(FileOut)
;
true
),
fail
).
qualify(a(_)).
qualify(b('Carlo')).
qualify(( c(X) :- a(X), b(X) )).
| |
| Carlo Capelli 2007-05-04, 4:10 am |
| An improvement to avoid the choice points (only one?) introduced by repeat.
Anyway the code is cleaner and more efficient... (thanks Markus!)
filter_stream(FileIn, FileOut) :-
repeat,
read(FileIn, T),
( qualify(T) ->
writeq(FileOut, T),
write(FileOut, '.'),
nl(FileOut)
; true
),
T == end_of_file,
!.
Although I wrote an interpreter (in early 90s, my first lovely C++), this
kind of misunderstanding of Prolog 'operational semantic' always scared
me...
Bye Carlo
"Carlo Capelli" <carlo.capelli@rdbos.it> ha scritto nel messaggio
news:MsB_h.29964$281.5878@tornado.fastwebnet.it...
> "behdad" <behdadkh@gmail.com> ha scritto nel messaggio
> news:1178219751.187955.41930@o5g2000hsb.googlegroups.com...
>
> You can try this code in SWI-Prolog: i don't known if it's ISO portable.
> You must obviously write your own qualify/1.
> Bye Carlo.
>
> filter(PathIn, PathOut) :-
> open(PathIn, read, FileIn),
> open(PathOut, write, FileOut),
> filter_stream(FileIn, FileOut),
> close(FileOut),
> close(FileIn).
>
> filter_stream(FileIn, FileOut) :-
> repeat,
> read(FileIn, T),
> ( T = end_of_file -> true
> ;
> ( qualify(T) ->
> writeq(FileOut, T),
> write(FileOut, '.'),
> nl(FileOut)
> ;
> true
> ),
> fail
> ).
>
> qualify(a(_)).
> qualify(b('Carlo')).
> qualify(( c(X) :- a(X), b(X) )).
>
>
|
|
|
|
|