[Question]
  [
Piet is an interesting programming language for a number of reasons. Today we will focus on one reason: the [roll](https://twoguysarguing.wordpress.com/2010/03/15/piet-roll-command/) command. The roll command was originally from PostScript and is a powerful way to manipulate the stack.
The roll command pops the top two elements of the stack and uses them as parameters. We'll call the first value popped `turns` and the second `depth`. A turn to depth n will take the topmost element of the stack, make it the nth element in the stack, and move each of the elements above it up one. If `turns` is negative this is done in the opposite direction. That is, the nth element is moved to the top and the other elements are moved down. This is repeated `abs(turns)` times.
## Challenge
Write a program or function that takes in a stack and returns that stack after executing a roll.
### Rules
* Input and output may be in a list, array, string with a delimiter, passed in one element at a time, or any other reasonable format. Output must be in the same format as the input.
* `depth` will never be negative and will never be greater than the length of the stack.
* The input stack will always contain at least two elements.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest answer in each language wins. As such, I will not be accepting an answer.
* [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
### Test Cases
```
in:  out:
2    
4    
1    3
2    4
3    1
4    2
5    5
6    6
in:  out:
-2   
3
1    2
2    3
3    1
in:  out:
-42
0
1    1
2    2
3    3
4    4
5    5
```
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~64~~ 62 bytes
Edit: -2 bytes: @xnor saw something I'd thought wrong about.
`r` takes and returns a list of `Int`s.
```
r(t:d:l)|d<1=l|(x,y)<-d%l,(z,w)<-mod t d%x=w++z++y
(%)=splitAt
```
[Try it online!](https://tio.run/nexus/haskell#RYq9CoMwFEb3PIVDhYRcQ021g5ihzyEOoVdoIEZJUvzBd7fq0uk75/D12jhlXOy8fsfb11njuiB6PdLwGSbhhe80MnHl3dNYYWXZhnWu7EZnWFidYWqBrjAd2A@YxATTWU2cr5wvhKZMhdGa@Ir73kgoIAcJj2NLeLakyU650imFhPv/0JIf "Haskell – TIO Nexus")
`splitAt n l` splits a list `l` at index `n`, `mod` calculates remainder of division, `++` concatenates lists.
[Answer]
## JavaScript (ES6), ~~49~~ 47 bytes
```
(t,d,...a)=>a.splice(t=(t%d+d)%d,d-t).concat(a)
```
Edit: Saved 2 bytes thanks to @Shaggy by taking the stack elements as separate parameters. Explanation:
* When the turn is a multiple of the depth, nothing happens. The first step is therefore to calculate turn modulo depth. As JavaScript only knows how to calculate the remainder, I have to do this in two steps.
* A turn of `1` moves the top element to the `depth` element. A turn of `2` moves the top two elements, etc. However, you can also achieve this by moving the elements between turn and depth to the front. `splice` removes those elements and `concat` prepends them to the remaining elements. (I could have used an array comprehension instead as it is the same length.)
* Unlike `slice`, the second parameter to `splice` is the number of elements to remove.
[Answer]
## CJam, 31 bytes
```
)\):N@\,0a|={NW*1$1$>)\+@@<\+}*
```
Input and output are arrays on the stack, with the last element representing the top of the stack.
Stack trace:
```
                   e# Stack:                [6 5 4 3 2 1 4 2]
)                  e# Take out first value: [6 5 4 3 2 1 4] 2
\                  e# Swap:                 2 [6 5 4 3 2 1 4]
)                  e# Take out first value: 2 [6 5 4 3 2 1] 4
:N                 e# Store in N:           2 [6 5 4 3 2 1] 4; N=4
@                  e# Rotate:               [6 5 4 3 2 1] 4 2
\                  e# Swap:                 [6 5 4 3 2 1] 2 4
,                  e# Range:                [6 5 4 3 2 1] 2 [0 1 2 3]
0                  e# Push 0:               [6 5 4 3 2 1] 2 [0 1 2 3] 0
a                  e# Wrap in array:        [6 5 4 3 2 1] 2 [0 1 2 3] [0]
|                  e# Logical or:           [6 5 4 3 2 1] 2 [0 1 2 3]
                   e# (This will replace an empty array with [0] to handle a special case of n=0)
=                  e# Get array value:      [6 5 4 3 2 1] 2
{NW*1$1$>)\+@@<\+} e# Push block:           [6 5 4 3 2 1] 2 {NW*1$1$>)\+@@<\+}
*                  e# Preform n times:      [6 5 4 3 2 1]
  N                e# Push N:               [6 5 4 3 2 1] 4
  W*               e# Negate:               [6 5 4 3 2 1] -4
  1$               e# Copy element 1 back:  [6 5 4 3 2 1] -4 [6 5 4 3 2 1]
  1$               e# Copy element 1 back:  [6 5 4 3 2 1] -4 [6 5 4 3 2 1] -4
  >                e# Slice a[-4:]          [6 5 4 3 2 1] -4 [4 3 2 1]
  )                e# Take first value:     [6 5 4 3 2 1] -4 [4 3 2] 1
  \                e# Swap:                 [6 5 4 3 2 1] -4 1 [4 3 2]
  +                e# Append:               [6 5 4 3 2 1] -4 [1 4 3 2]
  @@               e# Rotate twice:         [1 4 3 2] [6 5 4 3 2 1] -4
  <                e# Slice a[:-4]:         [1 4 3 2] [6 5]
  \                e# Swap:                 [6 5] [1 4 3 2]
  +                e# Append:               [6 5 1 4 3 2]
e# Preform the block again:                 [6 5 2 1 4 3]
```
[Answer]
## Mathematica, 58 50 bytes
*Edit: Thanks to Martin Ender for saving 8 bytes.*
```
Take[x={##3},#2]~RotateLeft~#~Join~Drop[x,#2]&@@#&
```
## Explanation:
Pure function which expects a list where the beginning of the list represents the top of the stack. We pass the elements of the list into the pure function `Take[x={##3},#2]~RotateLeft~#~Join~Drop[x,#2]&`. `x` is set to the sequence of elements starting with the third argument., then we rotate the first `#2` (second argument) elements of `x` to the left `#` (first argument) times, then `Join` the remaining elements of `x`. 
It would save `3` bytes if we just passed the stack elements in as arguments to the function directly rather than being in a list initially, but then the input and output formats wouldn't match.
## Original solution:
```
#/.{t_,d_,x___}:>{x}~Take~d~RotateLeft~t~Join~Drop[{x},d]&
```
There's something really satisfying about this chain of infix functions. Replaces a list with first element `t`, second element `d`, and remaining elements `x` with the result of rotating the first `d` elements of `{x}` to the left `t` times and joining the remaining elements of `{x}`.
[Answer]
# Ruby, 40 bytes
```
x=->s{n,d,*s=s;s[0,d]=s[0,d].rotate n;s}
```
[Try it online!](https://tio.run/nexus/ruby#@19hq2tXXJ2nk6KjVWxbbF0cbaCTEmsLofSK8ksSS1IV8qyLa/8XKFRERxvpmOgY6hjpGANpUx2z2FgusLAuSAQsDhcxMdIxQCiNjf3/HwA "Ruby – TIO Nexus")
Takes the input as a list, returns a list. The fact that a built-in `rotate` exists which can handle both positive and negative rotations makes this trivial.
[Answer]
# Python, ~~141~~ ~~98~~ ~~87~~ 74 bytes
*11 bytes saved thanks to @Cole*
```
def f(s):*s,d,t=s;n=len(s)-d;return s*0**d or s[:n]+s[-t%d-d:]+s[n:-t%d-d]
```
Receives input as a list, where the last element is the top of the stack.
Uses the 0ⁿ trick to filter zero-depth and python's sign-adjusting modulo operator to determine the part of the list to-be-chopped.
[Answer]
# JavaScript ES6, ~~109~~ 92 bytes
```
x=>{for(i=x.shift(),i=i>0?i:-i,j=x.shift();i-->0&&j>0;)x=x.splice(j,1).concat(x);return x}
```
[Try it online!](https://tio.run/nexus/javascript-node#bYzBCgIhFEX3fUWrQeEpOk0tEu1DokWI1htkDMdAiL7dnDYD0erCOYfrdS3avHxMBHXh8x19JhRQoxEnPDKEccVMKmTMiK4bjVC0LOYR0DoygqTcxsleMylUJZefadqWd21sjsHxEG/Ek3MPA0joYdd2D4cLpZufgi3ym/yTQw9iPWhF/QA)
Receives input in the form of an array of integers.  
Also has the count to arrow :P
## Explanation:
The code uses the shift function to extract the first two elements of the list.
It then gets the absolute value of the first element, which is the number of turns.
Since Javascript is zero indexed, the depth index needs to be decreased by 1.
If the depth index was 0 or 1, nothing should change, but due to the decrease, index of 0 would cause changes. Therefore exit the loop if depth index is not <=0.
The splice(a,b) function returns the sub-array of length b with starting index a from the array and leaves the original array without those elements.
When concatenated with the remainder of the original array, this is a single rotation of the array at the depth index.
By performing this operation n times, where n is the number of turns, the resulting array is the result of the roll operator.
[Answer]
# [Python 2](https://docs.python.org/2/), 48 bytes
```
lambda r:r[2:r[1]+2][r[0]:]+r[2:r[0]]+r[r[1]+2:]
```
[Try it online!](https://tio.run/nexus/python2#y7GN@Z@TmJuUkqhQZFUUbQTEhrHaRrHRRdEGsVax2hAhg1gQCyJlFfu/oCgzr0QhRyMzr6C0RENTk4ugwP9oIx0THUMdIx1jIG2qYxbLFa0L4oCFQBwTIx0DhIJYrq95@brJickZqQA "Python 2 – TIO Nexus")
[Answer]
# TI-Basic, ~~141~~ 150 bytes (noncompeting)
```
Prompt L1
L1(1→T
L1(2→D
seq(L1(C),C,3,dim(L1→L1
If TD>0
Then
For(A,1,T
L1(1→B
For(C,2,D
L1(C→L1(C–1
End
B→L1(D
End
End
If TD<0
Then
For(A,1,-T
L1(D→B
For(C,D,2,-1
L1(C–1→L1(C
End
B→L1(1
End
End
L1
```
Edit: fixed case where depth is zero (+9 bytes)
TI-Basic doesn't support 0-length lists, so this approach won't work for a two-length input.
Explanation:
```
Prompt L1                # 4 bytes, input list
L1(1→T                   # 7 bytes, turns
L1(2→D                   # 7 bytes, depth
seq(L1(C),C,3,dim(L1→L1   # 18 bytes, remove turns and depth from list
If TD>0                  # 6 bytes, if turns is positive and depth is nonzero (can't be negative)
Then                     # 2 bytes
For(A,1,T                # 7 bytes, do this 'turns' times
L1(1→B                    # 7 bytes, backup the first item
For(C,2,D                # 7 bytes, shuffle the rest along
L1(C→L1(C–1               # 12 bytes
End                      # 2 bytes
B→L1(D                   # 7 bytes, restore the backup to where it should be
End                      # 2 bytes
End                      # 2 bytes
If TD<0                  # 6 bytes, if T is negative and D is nonzero
Then                     # 2 bytes
For(A,1,-T               # 8 bytes, do this -'turns' times
L1(D→B                   # 7 bytes, backup the Dth item
For(C,D,2,-1             # 10 bytes, shuffle the items the other way
L1(C–1→L1(C              # 12 bytes
End                      # 2 bytes
B→L1(1                   # 7 bytes, restore backup to where it belongs
End                      # 2 bytes
End                      # 2 bytes
L1                       # 2 bytes, implicitly return
```
[Answer]
## Batch, 163 bytes
```
@set s=
@set r=
@set/ad=%2,t=(%1%%d+d)%%d
:l
@shift
@set/af=t-=1,f^^=d-=1
@if %f% lss 0 (set r=%r% %2)else set s=%s% %2
@if not "%3"=="" goto l
@echo%r%%s%
```
Takes input as command-line parameters and outputs a space-separated list. The parameters between `t` and `d` are extracted into the `r` variable so that they can be prepended to the `s` variable, which receives all of the other parameters.
]