Shift list elements to left by 3: Difference between revisions

m
(add PicoLisp)
Line 1,120:
 
rotated :: Int -> [a] -> [a]
rotated n xs =
( (<*>) take
take <*> (flip drop (cycle xs) . mod n) $ length xs
. flip (drop . mod n)
. cycle
)
<*> length
 
--------------------------- TEST -------------------------
Line 1,128 ⟶ 1,131:
main =
let xs = [1 .. 9]
in putStrLn "List rotated 3 or 30 positions to the left:"
>> print (rotated 3 xs)
>> print (rotated 30 xs)
>> putStrLn "\nList rotated 3 or 30 positions to the right:"
>> print (rotated (-3) xs)
>> print (rotated (-30) xs)</lang>
{{Out}}
<pre>------------- SHIFT LIST ELEMENTS TO LEFT BY N -----------
<pre>List rotated 3 positions to the left:
[4,5,6,7,8,9,1,2,3]
[4,5,6,7,8,9,1,2,3]
 
List rotated 3:: positionsInt to-> the[a] right:-> [a]
rotated n =
[7,8,9,1,2,3,4,5,6]
( (<*>) take
[7,8,9,1,2,3,4,5,6]</pre>
. flip (drop . mod n)
. cycle
)
<*> length
 
--------------------------- TEST -------------------------
main :: IO ()
main =
let xs = [1 .. 9]
<pre> in putStrLn "List rotated 3 or 30 positions to the left:"
>> print (rotated 3 xs)
>> print (rotated 30 xs)
>> putStrLn "\nList rotated 3 or 30 positions to the right:"
>> print (rotated (-3) xs)
>> print (rotated (-30) xs)</pre>
 
=={{header|Java}}==
9,655

edits