prolog permutation function

2019-07-17 00:35发布

I'm new in Prolog. I know the recursion function of permutation, that is:

per([],[]).
per(L, [X|P]) :- del(X,L,L1),per(L1,P).

I want to know the logic tree of this function in the final iteration when we receive per([],[]) which element it back.

标签: prolog
1条回答
时光不老,我们不散
2楼-- · 2019-07-17 01:32

Sonia - you haven't given us the 'del' predicate so we can't run it and show you.

Did you know there is a 'trace' predicate that dumps out every step/decision point/backtrack ?

Just do:

trace.
per([a,b], X).

Then press Enter until you see prolog match the base of the recursion ([], []), and then continue to press Enter to watch it complete the task.

If at any time you want to see the abbreviated lists that generated (ie lists shown ending with dots [..], then press 'w' - write to dump out the entire list contents.

then finally, do:

notrace.

To switch it off.

And then if you have specific questions about the trace, suggest adding in the missing predicate, and showing a partial trace around the question you have.

查看更多
登录 后发表回答