Shift list elements to left by 3: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Elixir)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(7 intermediate revisions by 4 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 943 ⟶ 944:
 
=={{header|Euler}}==
'''begin'''
<syntaxhighlight lang="euler">
'''new''' shl; '''new''' shl3;
begin
new shl; new shl3;
shl &lt;- ` '''formal''' ls;
 
'''if''' '''length''' ls &lt; 2 '''then''' ls
shl <- ` formal ls;
if length ls < 2 then ls '''else''' '''begin'''
else begin '''new''' L;
new L &lt;- ls;
L <-'''tail''' lsL &amp; ( L[ 1 ] )
tail L & ( L[ 1 ] )'''end'''
end&apos;;
shl3 &lt;- ` '''formal''' ls; shl( shl( shl( ls ) ) ) &apos;;
';
shl3 <- ` formal ls; shl( shl( shl( ls ) ) ) ';
'''out''' shl3( ( 1, 2, 3, 4, 5, 6, 7, 8 ) )
 
'''end'''
out shl3( ( 1, 2, 3, 4, 5, 6, 7, 8 ) )
$
end
$
</syntaxhighlight>
 
=={{header|Excel}}==
Line 1,156 ⟶ 1,155:
next i</syntaxhighlight>
{{out}}<pre>4, 5, 6, 7, 8, 9, 1, 2, 3</pre>
 
=={{header|Go}}==
{{trans|Wren}}
Line 1,224:
 
=={{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>
Line 1,330 ⟶ 1,328:
"123456789" | rotateLeft(3) #=> "456789123"
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,342 ⟶ 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,395 ⟶ 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,480 ⟶ 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,565 ⟶ 1,661:
 
=={{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
≫ ≫
'SLL3' STO
 
====Following ''Programming Pearls''' Problem B algorithm====
{ 1 2 3 4 5 6 7 8 } SLL3
{{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>
Line 1,599 ⟶ 1,702:
After: [4, 5, 6, 7, 8, 9, 1, 2, 3]
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
Line 1,628 ⟶ 1,732:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">// in place left shift by 1
var lshift = Fn.new { |l|
var n = l.count
Line 1,650 ⟶ 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
9,482

edits