开发者

a relation in Prolog to shift elements left rotationally

I need a relation in prolog to shift list left rotationally by one element such that

开发者_JS百科
?shift([1,2,3],L)

should produce

L=[2,3,1].

could you help me?


You can use the append command to combine elements of a list together:

shift([H|T], Y) :-
  append(T, [H], Y).

So you simply append the tail and head together (in that order), and set Y to that newly-created list. Note that since H is an element and not a list, you must surround it with [ and ] to make it a list in the append function.

Also, here's a good basic overview of using lists in Prolog.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜