问题是您append(R, F, X)
只使用未实例化的变量进行调用。
解决方案只有一种flt(+List, -Flatlist)
。然而,在回溯时,append(R, F, X)
一次又一次地尝试,却没有找到另一个解决方案......
您可以通过询问来测试
?- append(R, F, X).
R = [],
F = X ;
R = [_948],
X = [_948|F] ;
R = [_948, _960],
X = [_948, _960|F] ;
R = [_948, _960, _972],
X = [_948, _960, _972|F] ;
R = [_948, _960, _972, _984],
X = [_948, _960, _972, _984|F] ;
R = [_948, _960, _972, _984, _996],
X = [_948, _960, _972, _984, _996|F]
...
解决方案是重新安排第三个子句中的目标:
flt([H|L], X):-
flt(H, R),
flt(L, F),
append(R, F, X).
这是一个很好的例子,说明 Prolog 不是纯粹的声明性,因为它通过简单的时间回溯来实现解析,强制语言的程序特性。
为了说明这个问题,看看这个跟踪(我跳过了很多,只是为了强调循环问题):
[trace] ?- flt([[a]],X).
Call: (8) flt([[a]], _680) ? creep
^ Call: (9) not(is_list([a])) ? creep
^ Fail: (9) not(user:is_list([a])) ? creep
Redo: (8) flt([[a]], _680) ? creep
Call: (9) lists:append(_904, _906, _680) ? creep
Exit: (9) lists:append([], _680, _680) ? creep
Call: (9) flt([a], []) ? skip
Fail: (9) flt([a], []) ? creep
Redo: (9) lists:append(_904, _906, _680) ? creep
Exit: (9) lists:append([_890], _898, [_890|_898]) ? creep
Call: (9) flt([a], [_890]) ? skip
Exit: (9) flt([a], [a]) ? creep
Call: (9) flt([], _898) ? skip
Exit: (9) flt([], []) ? creep
Exit: (8) flt([[a]], [a]) ? creep
X = [a] ;
Redo: (9) flt([a], [_890]) ? skip
Fail: (9) flt([a], [_890]) ? creep
Redo: (9) lists:append([_890|_892], _918, [_890|_898]) ? creep
Exit: (9) lists:append([_890, _902], _910, [_890, _902|_910]) ? creep
Call: (9) flt([a], [_890, _902]) ? skip
Fail: (9) flt([a], [_890, _902]) ? creep
Redo: (9) lists:append([_890, _902|_904], _930, [_890, _902|_910]) ? creep
Exit: (9) lists:append([_890, _902, _914], _922, [_890, _902, _914|_922]) ? creep
Call: (9) flt([a], [_890, _902, _914]) ? skip
Fail: (9) flt([a], [_890, _902, _914]) ? creep
Redo: (9) lists:append([_890, _902, _914|_916], _942, [_890, _902, _914|_922]) ? creep
Exit: (9) lists:append([_890, _902, _914, _926], _934, [_890, _902, _914, _926|_934]) ? creep
Call: (9) flt([a], [_890, _902, _914, _926]) ? skip
Fail: (9) flt([a], [_890, _902, _914, _926]) ? creep
Redo: (9) lists:append([_890, _902, _914, _926|_928], _954, [_890, _902, _914, _926|_934]) ? creep
Exit: (9) lists:append([_890, _902, _914, _926, _938], _946, [_890, _902, _914, _926, _938|_946]) ? creep
Call: (9) flt([a], [_890, _902, _914, _926, _938]) ? skip
Fail: (9) flt([a], [_890, _902, _914, _926, _938]) ? creep
Redo: (9) lists:append([_890, _902, _914, _926, _938|_940], _966, [_890, _902, _914, _926, _938|_946]) ? creep
Exit: (9) lists:append([_890, _902, _914, _926, _938, _950], _958, [_890, _902, _914, _926, _938, _950|_958]) ? creep
Call: (9) flt([a], [_890, _902, _914, _926, _938, _950]) ? skip
Fail: (9) flt([a], [_890, _902, _914, _926, _938, _950]) ? creep
Redo: (9) lists:append([_890, _902, _914, _926, _938, _950|_952], _978, [_890, _902, _914, _926, _938, _950|_958]) ?
此图以图形方式显示它。要求额外答案后发生的事情被涂成红色。
另一种可能的解决方案是添加削减:
flt([], []).
flt([H|L], [H|X]):-
not(is_list(H)),
flt(L, X),
!.
flt([H|L], X):-
append(R, F, X),
flt(H, R),
!,
flt(L, F).