我是using a predicate在prolog程序shell中读取连续提示中的一些值,我希望用户在被要求输入时能够获得帮助消息。场景将是:
input = 'help'
,则显示帮助消息并再次询问相同的输入input /= 'help'
,请指定Value
并成功保留到目前为止我做了什么:
ask_input( Question, Value ) :-
write( Question ), % Please enter ... :
read( ReadValue ),
( ReadValue = 'help' ->
write( 'Help message...' ),
ask_input( Question, Value )
; Value = ReadValue
).
显然,上面的代码不起作用。它会在条件内ask_input
失败。
答案 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 ).