3 ms·
Why couldn't you do file I/O? Of course there's file I/O in Prolog, and I'd be really surprised if there wasn't in Turbo Prolog Here's I/O in SWI-Prolog: http
by YeGoblynQueenne 4y ago
Why couldn't you do file I/O? Of course there's file I/O in Prolog, and I'd be really surprised if there wasn't in Turbo Prolog
Here's I/O in SWI-Prolog:
https://www.swi-prolog.org/pldoc/man?section=IO https://www.swi-prolog.org/pldoc/man?section=IO
I find it hard to parse your comment in good faith. You seem to say that Prolog is hopeless as a language because you couldn't find out how to do file I/O in Turbo Prolog in the '80s, when file I/O is both entirely possible and very simple to do in Prolog. It's not the fault of the language if you couldn't figure it out 40 years ago.
Edit: the Turbo Prolog manual you linked to lists the file_str/2 predicate for file I/O on page 73. Did you try that?.
- mikewarot 4y agoI installed SWI prolog, and I can not figure out how to run the example code I found on the internet. Here's hello.pl: main :- write('This is a sample Prolog program'), write(' This program is written into hello.pl file'). Trying to run hello.pl Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.4) SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software. Please run ?- license. for legal details. . For online help and background, visit https://www.swi-prolog.org For built-in help, use ?- help(Topic). or ?- apropos(Word). . 1 ?- [hello.pl]. ERROR: Type error: `dict' expected, found `hello' (an atom) ERROR: In: ERROR: [13] throw(error(type_error(dict,hello),_15432)) ERROR: [11] '.'(hello,pl,_15466) at c:/program files/swipl/boot/dicts.pl:57 ERROR: [10] '<meta-call>'(user:user: ...) <foreign> ERROR: [9] toplevel_call(user:user: ...) at c:/program files/swipl/boot/toplevel.pl:1173 ERROR: ERROR: Note: some frames are missing due to last-call optimization. ERROR: Re-run your program in debug mode (:- debug.) to get more detail. 2 ?- Google seems to be no help at this point, it's taken about 2 hours to get to this point. This is typical of the frustration I've experienced in my past attempts to understand this language. You can express a bunch of facts and rules, great... but you can't actually write code to act on them, apparently. 8( ----------- Edit -- Success has been achieved C:\Program Files\swipl>bin\swipl -s hello.pl -g main -g halt This is a sample Prolog program This program is written into hello.pl file C:\Program Files\swipl> ---------- Even better... here's hello2.pl main :- write('This is a sample Prolog program'), write(' This program is written into hello.pl file'). . ?- main. ?- halt. and it works as expected C:\Program Files\swipl>bin\swipl hello2.pl This is a sample Prolog program This program is written into hello.pl file C:\Program Files\swipl>
- Avshalom 4y agoit is just [hello]. %or ['hello.pl']. %or ["hello.pl"]. the '.' has syntactic meaning outside of quotes. now to demonstrate file i/o: we make a file hello.pl that has :- use_module(library(readutil)). readmyfile(Filename) :- open(Filename,read, Stream), read_line_to_string(Stream,Input), write("The line is:"), nl, write(Input), nl, close(Stream), %or slurp the whole thing read_file_to_string(Filename,WholeFile,[encoding(text)]), %lets use the formatted output predicate format("The file ~s has the following contents ~n~s ~n",[Filename,WholeFile]). then launch swi and ?- [hello]. true. ?- readmyfile('hello.pl'). The line is: :- use_module(library(readutil)). The file hello.pl has the following contents ... ... ... true.
- deleted 4y ago[deleted]
- YeGoblynQueenne 4y ago>> ?- [hello.pl]. Can I ask, why did you type that?