🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1619

Re: Rotating lists (Was: (none))

#1619
From: Arjen Duursma
Date: Tue, 13 Oct 1992 11:11
51 lines
1715 bytes
tyamamot (Taro Yamamoto) writes:

>I am learning the language too. But, I got the following idea.
>I am looking for any more elegant solution...
Arent we all :-)

>FYI.

>-Taro Yamamot
>tyamamot@adobe.com

>rotate(List, Shift, RotatedList) :-
>	Shift > 0, !,
>		length(List, TotLen), Shift2 is TotLen - Shift,
>		rot(List, Shift2, RotatedList).
I would not recommend the use of length

>rotate(List, Shift, RotatedList) :-
>	Shift < 0, !,
>		Shift2 is -Shift,
>		rot(List, Shift2, RotatedList).			

>rot(List, Shift, RotatedList) :- !,
>	append(SubList1, SubList2, List), length(SubList2, Len), Len =:= Shift, !,
>	append(SubList2, SubList1, RotatedList).
or append
>rot(List, Shift, RotatedList) :-
>		length(List, Len),
>		Shift2 is Len - Shift, !, rot(List, Shift2, RotatedList).

To avoid length, just let the items of a list go by.
To avoid append, use difference lists.

Fast attempt (definately not tested):

rotate(List,Shift,RotatedList) :-
	Shift < 0,!,NewShift is -Shift,
	rotate(RotatedList,NewShift,List).
rotate(List,Shift,RotatedList) :-
	rot(Shift,List,RotatedList,L-L).

rot(0,L1,L2,L2-L1) :- !. /* this is the append */
rot(X,[H|T],RotatedList,Dh-[H|Dt]) :-
	NewX is X - 1,
	rot(NewX,T,RotatedList,Dh-Dt).

It should work both ways.

Greetings

Arjen
Technical Details
Message-ID: <578@tower1.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!disnet!arjen
References: <9210122012.AA08759@enuxha.eas.asu.edu> <tyamamot-131092150955@macj001.tokyo.japan.adobe.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!disnet!arjen
From: arjen@disnet.uucp (Arjen Duursma)
Newsgroups: comp.lang.prolog
Subject: Re: Rotating lists (Was: (none))
Message-ID: <578@tower1.uucp>
Date: 13 Oct 92 11:11:33 GMT
References: <9210122012.AA08759@enuxha.eas.asu.edu> <tyamamot-131092150955@macj001.tokyo.japan.adobe.com>
Organization: Disnet, IDE Maassluis, The Netherlands
Lines: 51