【问题标题】:How to read a stream in Eclipse CLP?如何在 Eclipse CLP 中读取流?
【发布时间】:2013-07-26 16:29:20
【问题描述】:

我是 Eclipse 新手,我一直在尝试读取文件流但没有成功。我通常在 SWI-Prolog 中使用的代码是这样的:

read_until_stop(File, [L|Lines]) :-
    read_line_to_codes(File, Codes),
    Codes \= end_of_file,
    atom_codes(L, Codes),
    L \= stop,
    !,
    read_until_stop(File, Lines).
read_until_stop(_, []).

read_line_to_codes 显然在 Eclipse 中不可用。有什么好的替代方案?

【问题讨论】:

    标签: prolog eclipse-clp


    【解决方案1】:

    正如Eclipse manual 所建议的那样,Eclipse 对应的将是

    read_line(Stream, String) :-
        read_string(Stream, end_of_line, _Length, String).
    

    不同之处在于read_string 返回实际字符​​串而不是代码列表,即不再需要atom_codes

    ?- read_string(input, end_of_line, Length, String).
          abcdefghi
          Length = 9
          String = "abcdefghi"
          yes.
    

    【讨论】:

      【解决方案2】:

      我认为 read_line_to_codes/2 可以很容易地在 ECLiPSe 中实现,但是为了效率重用可用的内置函数。您可以使用read_line/2。

      尝试定义

      :- use_module(library(util)).
      read_line_to_codes(S, L) :- read_line(S, L).
      

      或者直接调用 read_line...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        相关资源
        最近更新 更多