Shift list elements to left by 3: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 5 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 931 ⟶ 932:
Shifted left by 3 :[4, 5, 6, 7, 8, 9, 1, 2, 3]</pre>
 
=={{header|EulerElixir}}==
<syntaxhighlight lang="eulerelixir">
def rotate(enumerable, count) do
begin
{left, right} = Enum.split(enumerable, count)
new shl; new shl3;
right ++ left
end
 
rotate(1..9, 3)
shl <- ` formal ls;
# [4, 5, 6, 7, 8, 9, 1, 2, 3]
if length ls < 2 then ls
</syntaxhighlight>
else begin
new L;
L <- ls;
tail L & ( L[ 1 ] )
end
';
shl3 <- ` formal ls; shl( shl( shl( ls ) ) ) ';
 
=={{header|Euler}}==
out shl3( ( 1, 2, 3, 4, 5, 6, 7, 8 ) )
'''begin'''
end
'''new''' shl; '''new''' shl3;
$
</syntaxhighlight>
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,145 ⟶ 1,155:
next i</syntaxhighlight>
{{out}}<pre>4, 5, 6, 7, 8, 9, 1, 2, 3</pre>
 
=={{header|Go}}==
{{trans|Wren}}
Line 1,213 ⟶ 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,319 ⟶ 1,328:
"123456789" | rotateLeft(3) #=> "456789123"
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,331 ⟶ 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,342 ⟶ 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,374 ⟶ 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,459 ⟶ 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,544 ⟶ 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,578 ⟶ 1,702:
After: [4, 5, 6, 7, 8, 9, 1, 2, 3]
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
Line 1,596 ⟶ 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,620 ⟶ 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