どうしたの?
私はPrologでいくつかの本当に奇妙な問題を抱えています。
特定のインデックスでリスト内の要素を置き換える再帰ルールが常に機能するとは限りません。
私のルールは次のようになります。
% Base rule - Stops when index is 1 and replaces the head with the element.
replaceAtIndex(1, _element, [_|_tail], [_element|_tail]).
% Recursive rule - Enter recursion for tail as long as index is larger than 1.
replaceAtIndex(_index, _element, [_head|_tail], [_head|_new_tail]):-
_index > 1,
_new_index is _index - 1,
replaceAtIndex(_new_index, _element, _tail, _new_tail).
プログラム内からデバッガーを使用すると、インデックスが何であっても常に2番目のルールが呼び出されますが、プログラムの外部でまったく同じコマンドを実行すると、完全に機能します。インデックス1に到達しますが、2番目のルールを呼び出し、最初のルールをバックトラックして試行せず、最後まで失敗します...
replaceAtIndexを呼び出すルールは次のようになります。
level_replace_block_value(_x, _y, _value):-
current_level(_level_number, _width, _height, _blocks, _drawX, _drawY),
coordinates_to_index(_x, _y, _index),
_index_in_list is _index + 1, % the replaceAtIndex is not 0 terminated
replaceAtIndex(_index_in_list, _value, _blocks, _new_blocks),
retractall(current_level(_,_,_,_,_,_)),
assert(current_level(_level_number, _width, _height, _new_blocks, _drawX, _drawY),
graphics_update_block_value(_x, _y).
インデックスを111として呼び出しをデバッグしているとき。定数111に
置き換えているときは、機能します。_index_in_list
なぜそれが起こるのか、誰かが手がかりを持っているかもしれませんか?