2
votes

I was browsing the IO manual pages in swi-prolog and couldn't find a predicate to read integers (or numbers) from a file/stream. Didn't find anything on google either :|

I know how to write one, just wondering if there is something built-in since I guess that it would be faster.

2

2 Answers

3
votes

In SWI-Prolog, there is library(dcg_basics) in the "http" package. It has a DCG non-terminal number//1, and you can use it with library(pio), i.e., phrase_from_file/2, to read directly from a file (without having to read the whole file at once). There was some discussion on the mailing list to include library(dcg_basics) in the core libraries, if you are interested check the archives and send a mail to the list. I think it would be a very useful addition to the libraries.

2
votes

If you can split your input up into a char list for each number, the charsio library has this predicate:

read_from_chars(+Codes, -Term)

that will read a term from a list of chars without expecting a terminating period.

The readutil library has predicates for reading char lists from streams, for example:

read_stream_to_codes(+Stream, -Codes)

It should be straightforward to split the codes list into sublists (comma or space delimited ?)