I feel like I'm bashing my head against a wall with something that I assume should be easy. Perhaps my approach is incorrect. I definitely don't feel like I understand the concept behind I/O in Prolog. (Such as: what is the difference between a stream alias and the variable bound by open/3?) But I digress:
How do a read a file line-by-line in GNU Prolog? (So without access to the handy functions that SWI has.) I assume it has something to do with get_char/1 and peek_char/1 (to check for a terminating newline) but my attempts to implement a workable solution have failed so far.
Here is as far as I've gotten:
readl_as_list(ID, X):-
current_input(ID),
readl_as_list(X).
readl_as_list([X]):-
(peek_char(NextChar), ==(NextChar, '\n');
get_code(Char1),
append([X], [Char1], X),
readl_as_list(X)).
printl_list([]):-
!, nl.
printl_list([H|X]):-
put_code(H), printl_list(X).
Loading this into the interpreter, I get (empty lines removed for readability):
| ?- open('word_list.txt', read, ID).
ID = '$stream'(2)
yes
| ?- readl_as_list(ID, X).
ID = '$stream'(0)
X = [_] ?
% (interpreter pauses until I press return)
yes
| ?- printl_list(X).
X = []
yes
The lines don't necessarily have to be read in as character lists, but since my goal is to search through a list of words for ones that match certain conditions (no repeat letters, for example), that seemed the most sensible way to do it.