Prolog:显示shell提示符的帮助

时间:2012-10-16 02:22:15

标签: input prolog swi-prolog

我是using a predicate在prolog程序shell中读取连续提示中的一些值,我希望用户在被要求输入时能够获得帮助消息。场景将是:

  1. 要求输入
  2. 如果input = 'help',则显示帮助消息并再次询问相同的输入
  3. 如果input /= 'help',请指定Value并成功保留
  4. 到目前为止我做了什么:

    ask_input( Question, Value ) :-
        write( Question ), % Please enter  ... :
        read( ReadValue ),
        ( ReadValue = 'help' ->
            write( 'Help message...' ),
            ask_input( Question, Value )
        ;   Value = ReadValue
        ).
    

    显然,上面的代码不起作用。它会在条件内ask_input失败。

1 个答案:

答案 0 :(得分:0)

我这样做了,似乎有效:

ask_question( Question, Value ) :-
    write( Question ), nl,
    read( ReadValue ),
    ask_question2( Question, ReadValue, NewValue ),
    Value = NewValue.

ask_question2( Question, ReadValue, NewValue ) :-
    ReadValue = 'help',
    write( 'Help message ...' ), nl,
    ask_question( Question, NewValue ).

ask_question2( _, Value, Value ).