# Implement ΔList [duplicate]

Given a list of integers, such as {1, 4, 2, 8, 10}, TI-Basic's ΔList will determine the difference between every overlapping pair of items. In the given example, it would be {3, -2, 6, 2}.

Task:

Take a list of integers as input, in any reasonable way. All overlapping pairs {x, y} should become y - x, and the resulting list returned. It should always be one item shorter. You can assume the list will never be shorter than two items.

Test cases:

{1, 2}                  {1}
{1, 2, 4}               {1, 2}
{2, 1}                  {-1}
{2, 2, 2}               {0, 0}
{-1, 0, 0, -1}          {1, 0, -1}
{0, 1, 2, 1, 1, 0}      {1, 1, -1, 0, -1}


Other:

This is . Shortest answer (in bytes) per language wins!

• Is the {2, 2, 2} -> {0, 0, 0} testcase correct? Shouldn't the output be {0, 0}? – Delfad0r Apr 22 at 15:17
• @Delfad0r Fixed – Redwolf Programs Apr 22 at 15:18
• sub-problem of this challenge. – Giuseppe Apr 22 at 15:21
• Instead of overlapping pairs {x, y} becoming {y - x}, can they become {x - y}? e.g. {1, 4, 2, 8, 10} -> {-3, 2, -6, -2} – Aaron Miller Apr 22 at 15:44
• I've closed this as a duplicate of Different Way Forward, as this is essentially the same task, but to only do one step – caird coinheringaahing Apr 22 at 15:53

# V (vim), 33 26 25 bytes

qqjYkPJr-0C<c-r>=<c-r>"
<Esc>j@qq@qdd


Try it online!

Input as a newline separated list of integers.

-7 bytes from kops.

-1 more byte from kops.

## Explanation

qqjYpkk:s/\n/-
qq             start macro q
jYp          go down and duplicate the second line from the cursor
kk        move back to the original position
:s/\n/- replace newline with -
Current Line: n1-n2

C<c-r>=-(<c-r>")
C                cut current line, enter insert mode
<c-r>=          evaluate the following
-(<c-r>") negate n1-n2

<Esc>j@qq@qdd
<Esc>         exit insert mode
j        go 1 step down to the next values
@qq     recursively call macro q and end macro
@q   call macro q
dd delete last extra line

• -7 bytes – kops Apr 22 at 20:48
• @kops thanks. I should use Jr in my other answers as well. – Razetime Apr 23 at 3:26
• Found one more byte – kops Apr 23 at 5:38

# 05AB1E, 1 byte

¥


Try it online!

# R, 4 bytes

diff


Try it online!

# Jelly, 1 byte

I


Try it online!

Feel free to add your answers here.

• What is a 'trivial answer'? 5 bytes = 5 commands for APL, or 4 bytes = 4 functions for Jelly seems quite a lot... Are there special 'brownie points' for outgolfing a 'trivial answer'? – Dominic van Essen Apr 22 at 15:44
• @DominicvanEssen Hmm, you're right. Maybe I should remove the APL and Jelly (and maybe the Husk) answers, at least for now? – user Apr 22 at 15:48
• They are pretty simple, and that's mostly why they are there. You can probably find most of these as part of many, many answers. – Razetime Apr 22 at 15:49
• A "trivial answers" CW answer should consist of just builtin answers. Not only does this not contain it, but it doesn't even have the literal Jelly builtin for this – caird coinheringaahing Apr 22 at 15:49
• I've removed them now – user Apr 22 at 15:56

# Retina 0.8.2, 82 bytes

\d+
$* ¶.*$&-$% ^.*¶ %(^((-1+)|(1*)-)-(1*)$
$2$3$4 (1*)-+\1 - --|-$

11*.&


Try it online! Takes input on separate lines but test suite converts from and to comma delimited for convenience. Explanation:

\d+
$*  Convert to unary. ¶.*$&-$%  Create expressions for the differences of successive terms. $% grabs the previous term without having to match it.

^.*¶



Delete the first term.

%(


Perform the rest of the script individually on each subtraction.

^((-1+)|(1*)-)-(1*)2$3$4


If the two values have different signs then negate the second and add it to the first.

(1*)-+\1
-


Otherwise subtract the two values.

--|-$ The subtraction can result in a double negative or a trailing subtraction of zero which needs to be removed. 11*$
\$.&


Convert to decimal.

• This question's been closed for nearly four hours (nice answer though!) – Redwolf Programs Apr 22 at 19:54

# JavaScript (ES6), 32 bytes

a=>a.slice(1).map((v,i)=>v-a[i])


Try it online!

### JavaScript (ES6), 33 bytes

a=>a.map(v=>-p+(p=v),p=a.shift())


Try it online!

### JavaScript (ES10), 34 bytes

a=>a.flatMap((v,i)=>i?v-a[i-1]:[])


Try it online!

• I don't think it can get much shorter than this... – Recursive Co. Apr 22 at 15:32

# Python 3, 38 bytes

lambda s:[y-x for x,y in zip(s,s[1:])]


Try it online!

-8 bytes thanks to @cairdcoinheringaahing

# Charcoal, 10 bytes

ＩＥΦθκ⁻ι§θκ


Try it online! Link is to verbose version of code. Explanation:

   θ        Input array
Φ         Filtered where
κ       Current index is not zero
Ｅ          Map over remaining elements
ι     Current element
⁻      Subtract
θ   Input array (still with zeroth element)
§    Indexed by
κ  Current index
Ｉ           Cast to string
Implicitly print


# APL 5 bytes

2-⍨/⎕

⎕ ⍝ input
-⍨   ⍝ reversed subtraction
2  /  ⍝ between every 2 elements like [1,2,3] -> [1 f 2, 2 f 3]


try it online

# Haskell, 17 bytes

zipWith(-)=<<tail


Try it online!

# R, 25 bytes

head((x=scan())[-1]-x,-1)


Try it online!

The R 'trivial' answer is diff (difference).

So this is my attempt at the golfiest solution without diff...

• There are lots of longer ones without diff, including my favorite, but I haven't been able to find a shorter one. – Giuseppe Apr 22 at 18:12

# Jelly, 2 bytes

IƝ


Try it online!

Full program, as it relies on Jelly's outputting format.

This applies I (forward differences) to each overlapping pair [x, y] (Ɲ), yielding [y - x] for each. Due to Jelly's printing, as a full program, it omits the brackets around single element lists, so the output looks like a flat list.

# Jelly, 3 bytes

Non-builtin approaches. All are functions

_ƝN


Try it online!

_@Ɲ


Try it online!

IƝF


Try it online!

## How they work

They all use the Ɲ quick, which takes a link before it (a command, usually an atom) and a list, and applies the link to all overlapping pairs in the list.

_ is subtraction in Jelly, and @ reverses the order of the arguments to the previous link.

The first one calculates x - y for each overlapping pair, then negates each.

The second calculates x -@ y for each overlapping pair, which is equivalent to y - x

The third applies I (forward differences) to each pair, yielding [y - x]. We then flatten this into a single array.

• Hm... Just when I was wondering what did and what did not qualify as a 'trivial' answer... – Dominic van Essen Apr 22 at 15:47
• @DominicvanEssen I didn't edit this into the CW answer because I think they should be for builtin-only answers, which this one clearly isn't. If it were edited to remove the APL and Jelly answers, I might – caird coinheringaahing Apr 22 at 15:48
• (only the first one - I - was there when I commented: I assume that counts as a built-in, right? The others all look perfectly valid to me... well done [unless there's a 2-byter that you're missing...]) – Dominic van Essen Apr 22 at 15:54
• @DominicvanEssen Yeah, I don't think you can get more "builtin" than 1 byte in Jelly :P – caird coinheringaahing Apr 22 at 15:56

# Wolfram Language (Mathematica), 11 bytes

Differences
`

Try it online!