Shift list elements to left by 3: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(16 intermediate revisions by 7 users not shown)
Line 9:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F rotate(l, n)
V k = (l.len + n) % l.len
Line 621 ⟶ 620:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">lst: [1 2 3 4 5 6 7 8 9]
print rotate.left lst 3</syntaxhighlight>
 
{{out}}
 
<pre>4 5 6 7 8 9 1 2 3</pre>
 
Line 679 ⟶ 676:
new: b;c;d;a
</pre>
 
 
 
=={{header|BASIC}}==
Line 802 ⟶ 797:
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">data ← 1 + ↕9
 
3 ⌽ data</syntaxhighlight>
{{out}}
<pre>⟨ 4 5 6 7 8 9 1 2 3 ⟩</pre>
 
=={{header|C}}==
Line 930 ⟶ 931:
<pre>Original list :[1, 2, 3, 4, 5, 6, 7, 8, 9]
Shifted left by 3 :[4, 5, 6, 7, 8, 9, 1, 2, 3]</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
def rotate(enumerable, count) do
{left, right} = Enum.split(enumerable, count)
right ++ left
end
 
rotate(1..9, 3)
# [4, 5, 6, 7, 8, 9, 1, 2, 3]
</syntaxhighlight>
 
=={{header|Euler}}==
'''begin'''
'''new''' shl; '''new''' shl3;
shl &lt;- ` '''formal''' ls;
'''if''' '''length''' ls &lt; 2 '''then''' ls
'''else''' '''begin'''
'''new''' L;
L &lt;- ls;
'''tail''' L &amp; ( L[ 1 ] )
'''end'''
&apos;;
shl3 &lt;- ` '''formal''' ls; shl( shl( shl( ls ) ) ) &apos;;
'''out''' shl3( ( 1, 2, 3, 4, 5, 6, 7, 8 ) )
'''end'''
$
 
=={{header|Excel}}==
Line 1,125 ⟶ 1,155:
next i</syntaxhighlight>
{{out}}<pre>4, 5, 6, 7, 8, 9, 1, 2, 3</pre>
 
=={{header|Go}}==
{{trans|Wren}}
Line 1,191 ⟶ 1,222:
[7,8,9,1,2,3,4,5,6]
[7,8,9,1,2,3,4,5,6]</pre>
 
=={{header|J}}==
<syntaxhighlight lang=J> 3 |. 1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3</syntaxhighlight>
 
However, while this matches the task example, it does not match the task title. In most contexts, a shift is different from a rotate:
<syntaxhighlight lang=J> 3 |.(!.0) 1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 0 0 0</syntaxhighlight>
 
=={{header|Java}}==
Line 1,289 ⟶ 1,328:
"123456789" | rotateLeft(3) #=> "456789123"
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,301 ⟶ 1,341:
{{out}}
<pre>{4, 5, 6, 7, 8, 9, 1, 2, 3}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
list:[1,2,3,4,5,6,7,8,9]$
append(rest(list,3),rest(list,-(length(list)-3)));
</syntaxhighlight>
{{out}}
<pre>
[4,5,6,7,8,9,1,2,3]
</pre>
 
=={{header|Nim}}==
Line 1,312 ⟶ 1,362:
{{out}}
<pre>@[1, 2, 3, 4, 5, 6, 7, 8, 9] → @[4, 5, 6, 7, 8, 9, 1, 2, 3]</pre>
 
=={{header|OCaml}}==
At the [[REPL]]:
<syntaxhighlight lang="ocaml"># let rec rotate n = function
| h :: (_ :: _ as t) when n > 0 -> rotate (pred n) (t @ [h])
| l -> l
;;
val rotate : int -> 'a list -> 'a list = <fun>
# rotate 3 [1; 2; 3; 4; 5; 6; 7; 8; 9] ;;
- : int list = [4; 5; 6; 7; 8; 9; 1; 2; 3]</syntaxhighlight>
 
=={{header|Perl}}==
Line 1,344 ⟶ 1,404:
<syntaxhighlight lang="picolisp">(let L (range 1 9)
(println (conc (tail -3 L) (head 3 L))) )</syntaxhighlight>
 
=={{header|PL/0}}==
PL/0 has no data structures, not even arrays. This sample simulates an array using separate variables for each element. The simulated array is then used to represent the list.
<br>
The output shows the original list (one element per line) and the shifted list, separated by -111.
<br>
Note that PL/0 was intended as an educational tool for teaching compiler writing - so, e.g.: adding arrays might be an exercise for the students.
<syntaxhighlight lang="pascal">
const maxlist = 8;
var sub, v, l1, l2, l3, l4, l5, l6, l7, l8;
procedure getelement;
begin
if sub = 1 then v := l1;
if sub = 2 then v := l2;
if sub = 3 then v := l3;
if sub = 4 then v := l4;
if sub = 5 then v := l5;
if sub = 6 then v := l6;
if sub = 7 then v := l7;
if sub = 8 then v := l8;
end;
procedure setelement;
begin
if sub = 1 then l1 := v;
if sub = 2 then l2 := v;
if sub = 3 then l3 := v;
if sub = 4 then l4 := v;
if sub = 5 then l5 := v;
if sub = 6 then l6 := v;
if sub = 7 then l7 := v;
if sub = 8 then l8 := v;
end;
procedure shiftleft;
var first;
begin
sub := 1;
call getelement;
first := v;
sub := 0;
while sub < maxlist do begin
sub := sub + 2;
call getelement;
sub := sub - 1;
call setelement
end;
sub := maxlist;
v := first;
call setelement
end;
begin
sub := 0;
while sub < maxlist do begin
sub := sub + 1;
v := sub;
call setelement;
! v
end;
call shiftleft;
call shiftleft;
call shiftleft;
! -111;
sub := 0;
while sub < maxlist do begin
sub := sub + 1;
call getelement;
! v
end
end.
</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
7
8
-111
4
5
6
7
8
1
2
3
</pre>
 
=={{header|Python}}==
Line 1,429 ⟶ 1,578:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">' [ 1 2 3 4 5 6 7 8 9 ] 3 split swap join echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 4 5 6 7 8 9 1 2 3 ]</pre>
 
Line 1,511 ⟶ 1,658:
[4, 5, 6, 7, 8, 9, 1, 2, 3]
done...
</pre>
 
=={{header|RPL}}==
====Using the stack (idiomatic)====
≪ LIST→ → size
≪ <span style="color:red">1 3</span> '''START''' size ROLL '''NEXT'''
size →LIST
≫ ≫ '<span style="color:blue">SLL3</span>' STO
 
====Following ''Programming Pearls''' Problem B algorithm====
{{works with|HP|48G}}
≪ <span style="color:red">3</span> DUP2 <span style="color:red">1</span> SWAP SUB REVLIST
ROT ROT <span style="color:red">1</span> + OVER SIZE SUB REVLIST
+ REVLIST
≫ ‘<span style="color:blue">SLL3</span>’ STO
 
{ 1 2 3 4 5 6 7 8 } <span style="color:blue">SLL3</span>
{{out}}
<pre>
1: { 4 5 6 7 8 1 2 3 }
</pre>
 
Line 1,535 ⟶ 1,702:
After: [4, 5, 6, 7, 8, 9, 1, 2, 3]
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
Line 1,553 ⟶ 1,721:
<pre>(list-rotate '(1 2 3 4 5 6 7 8 9) 3) --> (4 5 6 7 8 9 1 2 3)
(list-rotate '(1 2 3 4 5 6 7 8 9) -3) --> (7 8 9 1 2 3 4 5 6)</pre>
 
=={{header|sed}}==
<syntaxhighlight lang="sed"># rotate in 3 steps to easily work with lengths < 4
s/^\([^,]*\),\(.*\)/\2,\1/
s/^\([^,]*\),\(.*\)/\2,\1/
s/^\([^,]*\),\(.*\)/\2,\1/</syntaxhighlight>
{{out}}
<pre>$ echo 1,2,3,4,5,6,7,8,9 | sed -f rotate.sed
4,5,6,7,8,9,1,2,3</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">// in place left shift by 1
var lshift = Fn.new { |l|
var n = l.count
Line 1,577 ⟶ 1,754:
{{libheader|Wren-seq}}
Alternatively, using a library method to produce the same output as before.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
var l = (1..9).toList
Line 1,598 ⟶ 1,775:
<pre>
4, 5, 6, 7, 8, 9, 1, 2, 3
</pre>
 
=={{header|Z80 Assembly}}==
===Zilog Z80===
This example code will not work on the Game Boy, as the Game Boy does not have the <code>RLD</code> or <code>RRD</code> instructions.
 
The <code>RLD</code> instruction is a very peculiar one. It rotates leftward the bottom four bits of the accumulator with the byte pointed to by HL. For example, if <code>A = &36</code> and <code>(HL) = &BF</code>, then a single <code>RLD</code> will result in <code>A = &3B</code> and <code>(HL) = &F6</code>. As it turns out, this can be used to rotate the entries in an array without using a ton of stack space.
 
<syntaxhighlight lang="z80">PrintChar equ &BB5A ;address of Amstrad CPC firmware routine, writes accumulator to stdout.
 
org &1000
ld hl,myarray_end-1 ;HL must point to the last byte in the array.
ld b,9 ;byte count
call RLCA_RANGE
 
ld hl,myarray_end-1
ld b,9
call RLCA_RANGE
 
ld hl,myarray_end-1
ld b,9
call RLCA_RANGE
 
ld hl,myarray
jp PrintString
;ret
 
myarray:
;pre-convert these numbers to ascii so that the job is easier.
byte &31,&32,&33,&34,&35,&36,&37,&38,&39
myarray_end:
byte 0 ;null terminator
 
RLCA_RANGE: ;DOESN'T WORK ON GAME BOY
;rotates a range of memory, e.g. &1000 = &23,&45,&67,&89 > &45,&67,&89,&23
;point HL at the end of the memory range this time.
ld a,(hl)
push hl
push bc
loop_RLCA_RANGE_FIRST:
RLD
DEC HL
djnz loop_RLCA_RANGE_FIRST
pop bc
pop hl
push hl
push bc
loop_RLCA_RANGE_SECOND:
RLD
DEC HL
djnz loop_RLCA_RANGE_SECOND
pop bc
pop hl
RLD
ret
 
PrintString:
ld a,(hl)
or a
ret z
call PrintChar
inc hl
jr PrintString</syntaxhighlight>
 
{{out}}
<pre>
456789123
</pre>
9,482

edits