prologb-prolog

Reading numbers from stdio in B-Prolog


What's the easiest way to read a number or several space-separated numbers (NOT followed by a period) in B-Prolog from standard input?

For example, for ECliPse I wrote these simple predicates (I don't need error handling):

read_number(N) :-
    read_token(Token, _),
    (
        Token == - 
    ->
        read_token(Nabs, _),
        N is -1 * Nabs
    ;
        N is Token
    ).

read_numbers_list(Ns) :-
    read_string(end_of_line, _, String),
    split_string(String, " ", "", Ss),
    ( foreach(S, Ss), foreach(N, Ns) do
        number_string(N, S) ).

But writing something like this for B-Prolog looks overly complicated to me - there are no read_token, or split_string...

It there an easy way for such a mundane task? Maybe some standard predicate I somehow overlooked?


Solution

  • I ended up with just preprocessing input outside of Prolog and then using read/1.

    Also, info from B-Prolog creator Neng-Fa Zhou (https://groups.google.com/forum/?fromgroups#!topic/comp.lang.prolog/KSXc2QTWZck):

    You can use an internal predicate, called read_next_token(Type,Val), for your purpose. Another predicate, called b_NEXT_TOKEN_ff(Type,Val), does the same thing but is faster. These predicates are not documented.