Prolog: Rotate list n times right

后端 未结 1 395
Happy的楠姐
Happy的楠姐 2021-01-21 05:02

Working on a predicate, rotate(L,M,N), where L is a new list formed by rotating M to the right N times.

My approach w

相关标签:
1条回答
  • 2021-01-21 05:57

    You can use append to split lists, and length to create lists:

    % rotate(+List, +N, -RotatedList)
    % True when RotatedList is List rotated N positions to the right
    rotate(List, N, RotatedList) :-
        length(Back, N),           % create a list of variables of length N
        append(Front, Back, List), % split L
        append(Back, Front, RotatedList).
    

    Note: this only works for N <= length(L). You can use arithmetic to fix that.

    Edit for clarity This predicate is defined for List and N arguments that are not variables when the predicate is called. I inadvertently reordered the arguments from your original question, because in Prolog, the convention is that strictly input arguments should come before output arguments. So, List and N and input arguments, RotatedList is an output argument. So these are correct queries:

    ?- rotate([a,b,c], 2, R).
    ?- rotate([a,b,c], 1, [c,a,b]).
    

    but this:

    ?- rotate(L, 2, [a,b,c]).
    

    will go into infinite recursion after finding one answer.

    When reading the SWI-Prolog documentation, look out for predicate arguments marked with a "?", as in length. They can be used as shown in this example.

    0 讨论(0)
提交回复
热议问题