开发者

a relation in Prolog to shift elements left rotationally

开发者 https://www.devze.com 2022-12-14 21:46 出处:网络
I need a relation in prolog to shift list left rotationally by one element such that开发者_JS百科

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

精彩评论

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