Writing in file | Swi-Prolog | Windows

后端 未结 2 1037
借酒劲吻你
借酒劲吻你 2021-01-19 12:41

Some script is working successful by swi-prolog 5.11.11 on linux-system, but not so well on windows-system by Swi-Prolog 5.6.48

main :-
    open(\'output.txt         


        
相关标签:
2条回答
  • 2021-01-19 12:45

    If one wanted to keep the close(OS) within a "single" main/0 clause, this also works:

    main :-
        open('output.txt',write,OS),
        (   elements(Points),
            get_set(Eq, Points),
            alpha_isotone(Eq, Points),
            write(OS,Eq),nl(OS),
            false
            ;
            close(OS)
        ).
    

    This syntax is not recommended as it is hard to remember what is the precedence of the conjunction , versus disjunction ; unless you frequently use such coding, and it is on that account less readable than the version presented by larsmans.

    Prolog defines the relative precedence of operators, even for AND, OR, and "neck" :-, by assigning Precedence values to each (users do this with op/3 when defining their own operators), ranging from 0 to 1200.

    There is also a reversal of the usual convention here, in that we normally mean the higher precedence operator is to be applied before lower precedence operators. But in Prolog the lower Precedence value indicates an operator binds (is applied) first.

    Actual precedence values vary with the implementation, but conjunction , has lower Precedence value than disjunction ; and thus binds first.

    0 讨论(0)
  • 2021-01-19 12:45

    I'm assuming this was meant to be a failure-driven loop. It doesn't work because the close/1 call, as @hardmath says, is never reached since it is preceded by a fail/1 in the same clause. In effect, the output file is apparently not flushed. This failure-driven loop should be written as:

    main :-
        open('output.txt', write, OS),
        main(OS).
    
    main(OS) :-
        elements(Points),
        get_set(Eq, Points),
        alpha_isotone(Eq, Points),
        write(OS,Eq), nl(OS),
        false.
    main(OS) :- close(OS).
    
    0 讨论(0)
提交回复
热议问题