Re: Four Easy Question
From: Martin Sondergaard (nobody_at_nowhere.com)
Date: 03/23/04
- Next message: Daniel C. Wang: "Re: Postdoc position in program development, analysis and transformation"
- Previous message: Georgios Tagalakis: "Re: Error with callable"
- In reply to: Linux Man: "Four Easy Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 23 Mar 2004 21:34:02 -0000
Linux Man wrote :
> Hi
>
> I'm new in prolog and have some easy question:
>
> 1- How can I create a file in prolog?
>
> 2- How can I read from a file or check that, say "john is father of
> bob" (father(john, bob)), from that file?
>
> 3- How can I write a new predicate in the created file
> (e.g. father(jim, alice).)?
>
>
> Many Thanks
> Steve
Another way of doing it, instead of reading each line from the file,
one line at a time,
is to read in the whole file by using "consult( 'myfile' )".
Also, you can write all your Prolog facts out to a file by using "listing",
instead of using "write" statements.
E.g.
write_to_file :-
tell( 'myfile.pl' ), % Open the file.
listing(father), % Write facts.
listing(mother),
listing(sibling),
told. % Close the file.
read_from_file :-
consult( 'myfile.pl' ).
To understand the above, learn about "listing/1",
and try it out.
In the code above, I used "tell" and "told" instead of "open" and
"close".
But you can use "open" and "close" if you prefer.
> 3- How can I write a new predicate in the created file
> (e.g. father(jim, alice).)?
First read in the old file, using "consult( 'myfile.pl' )".
Then assert the new fact, with "assert( father(jim, alice))".
Assert any new facts you want, and retract any facts you want to delete.
Then delete the old file, and write all the facts out to a new file.
?- consult( 'myfile.pl' ).
yes
?- assert(father(jim, alice)).
yes
?- assert( ... ). Etc.
?- delete_file( 'myfile.pl' ), write_to_file.
-- Martin Sondergaard, London.
- Next message: Daniel C. Wang: "Re: Postdoc position in program development, analysis and transformation"
- Previous message: Georgios Tagalakis: "Re: Error with callable"
- In reply to: Linux Man: "Four Easy Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|