[Question]
  [
Given a nonempty list of nonnegative integers, consider rewriting it as an arithmetic problem where:
* A plus sign (`+`) is inserted between pairs of numbers that increase from left to right (a.k.a. from the start of the list to the end).
* A minus sign (`-`) is inserted between pairs of numbers that decrease from left to right.
* A multiplication sign (`*`) is inserted between pairs of numbers that are equal.
Said another way: any sublist `a,b` becomes `a+b` if `a<b`, `a-b` if `a>b`, and `a*b` if `a==b`.
For example, the list
```
[12, 0, 7, 7, 29, 10, 2, 2, 1]
```
would become the expression
```
12 - 0 + 7*7 + 29 - 10 - 2*2 - 1
```
which [evaluates to](http://www.wolframalpha.com/input/?i=12+-+0+%2B+7*7+%2B+29+-+10+-+2*2+-+1) `75`.
Write a program or function that takes in such a list and evaluates it, printing or returning the result.
* Order of operations matters. Multiplications should be done before any addition or subtraction.
* If the input list has one number, that should be what it evaluates to. e.g. `[64]` should give `64`.
* Use of `eval` or `exec` or similar constructs is allowed.
Here are some additional examples:
```
[list]
expression
value
[0]
0
0
[1]
1
1
[78557] 
78557
78557
[0,0]
0*0
0
[1,1]
1*1
1
[2,2]
2*2
4
[0,1]
0+1
1
[1,0]
1-0
1
[1,2]
1+2
3
[2,1]
2-1
1
[15,4,4]
15-4*4
-1
[9,8,1]
9-8-1
0
[4,2,2,4]
4-2*2+4
4
[10,9,9,12]
10-9*9+12
-59
[1,1,2,2,3,3]
1*1+2*2+3*3
14
[5,5,4,4,3,3]
5*5-4*4-3*3
0
[3,1,4,1,5,9,2,6,5,3,5,9]
3-1+4-1+5+9-2+6-5-3+5+9
29
[7637,388,389,388,387,12,0,0,34,35,35,27,27,2]
7637-388+389-388-387-12-0*0+34+35*35-27*27-2
7379
```
**The shortest code in bytes wins. Tiebreaker is earlier answer.**
  
[Answer]
## Python 2, 63 bytes
```
p=s='print-'
for x in input():s+='*+-'[cmp(x,p)]+`x`;p=x
exec s
```
Constructs and `eval`s the expression string. The arithmetic symbol is chosen by comparing the previous number `p` to the current one `x`. The symbol is appended followed by the current number.
The first number is handled with a clever trick from Sp3000. The initial value of `p` is set to a string, which is bigger than any number and therefore causes a `-` before the first number. But, `s` is initialized to `print-` at the same time that makes the result start with `print--` (thanks to xsot for saving 2 bytes by initializing with `print`.)
[Answer]
# Pyth, ~~31~~ ~~26~~ ~~19~~ ~~17~~ ~~16~~ 15 bytes
Expressions with `*` won't evaluate online, but they would theoretically work.
2 bytes thanks to Maltysen.
```
vsm+@"*-+"._-~k
```
[Test suite (with evaluation).](http://pyth.herokuapp.com/?code=vsm%2B%40%22%2a-%2B%22._-%7Ek&test_suite=1&test_suite_input=%5B0%5D%0A%5B1%5D%0A%5B78557%5D%0A%5B0%2C1%5D%0A%5B1%2C0%5D%0A%5B1%2C2%5D%0A%5B2%2C1%5D%0A%5B9%2C8%2C1%5D%0A%5B3%2C1%2C4%2C1%2C5%2C9%2C2%2C6%2C5%2C3%2C5%2C9%5D&debug=0)
[The other cases (without evaluation).](http://pyth.herokuapp.com/?code=sm%2B%40%22%2a-%2B%22._-%7Ek&test_suite=1&test_suite_input=%5B0%2C0%5D%0A%5B1%2C1%5D%0A%5B2%2C2%5D%0A%5B15%2C4%2C4%5D%0A%5B4%2C2%2C2%2C4%5D%0A%5B10%2C9%2C9%2C12%5D%0A%5B1%2C1%2C2%2C2%2C3%2C3%5D%0A%5B5%2C5%2C4%2C4%2C3%2C3%5D%0A%5B7637%2C388%2C389%2C388%2C387%2C12%2C0%2C0%2C34%2C35%2C35%2C27%2C27%2C2%5D&debug=0)
## History
* 31 bytes: `M+G@"*-+"->GH<GHv+sgMC,JsMQtJ\x60e`
* 26 bytes: `M+G@"*-+"->GH<GHv+sgVQtQ\x60e`
* 19 bytes: `vtssVm@"*-+"->Zd<~Z`
* 17 bytes: `vtssVm@"*-+"._-~Z`
* 16 bytes: `vssVm@"*-+"._-~k`
* 15 bytes: `vsm+@"*-+"._-~k`
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), ~~18~~ ~~16~~ ~~15~~ 14 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
I0;ð1g×⁹⁸œṗP€S
```
Uses no built-in eval. [Try it online!](http://jelly.tryitonline.net/#code=STA7w7AxZ8OX4oG54oG4xZPhuZdQ4oKsUw&input=&args=Wzc2MzcsMzg4LDM4OSwzODgsMzg3LDEyLDAsMCwzNCwzNSwzNSwyNywyNywyXQ) or [verify all test cases](http://jelly.tryitonline.net/#code=STA7w7AxZ8OX4oG54oG4xZPhuZdQ4oKsUwrDh-KCrEc&input=&args=WzBdLCBbMV0sIFs3ODU1N10sIFswLDBdLCBbMSwxXSwgWzIsMl0sIFswLDFdLCBbMSwwXSwgWzEsMl0sIFsyLDFdLCBbMTUsNCw0XSwgWzksOCwxXSwgWzQsMiwyLDRdLCBbMTAsOSw5LDEyXSwgWzEsMSwyLDIsMywzXSwgWzUsNSw0LDQsMywzXSwgWzMsMSw0LDEsNSw5LDIsNiw1LDMsNSw5XSwgWzc2MzcsMzg4LDM4OSwzODgsMzg3LDEyLDAsMCwzNCwzNSwzNSwyNywyNywyXQ).
### How it works
```
I0;ð1g×⁹⁸œṗP€S  Main link. Input: A (list)
I               Increments; compute the deltas of all adjacent items of A.
 0;             Prepend a 0.
   ð            Begin a new, dyadic chain.
                Left argument: D (0 plus deltas). Right argument: A
    1g          Compute the GCD of 1 and each item in D.
                This yields 1 for non-negative items, -1 for negative ones.
      ×⁹        Multiply each 1 or -1 with the corresponding item of A.
                This negates every item in A that follows a - sign.
        ⁸œṗ     Partition the result by D. This splits at occurrences of non-zero
                values of D, grouping items with identical absolute value.
           P€   Take the product of each group.
             S  Sum; add the results.
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 12 bytes
```
Y'^l6MdZSh*s
```
This uses [@aditsu's very nice idea](https://codegolf.stackexchange.com/a/82725/36398) of run-length encoding.
[**Try it online!**](http://matl.tryitonline.net/#code=WSdebDZNZFpTaCpz&input=Wzc2MzcsMzg4LDM4OSwzODgsMzg3LDEyLDAsMCwzNCwzNSwzNSwyNywyNywyXQ)
### Explanation
```
       % Take input vector implicitly
Y'     % RLE. Produces two vectors: values and lengths
^      % Rise each value to the number of consecutive times it appears. This
       % realizes the product of consecutive equal values
l      % Push 1
6M     % Push vector of values again
d      % Consecutive differences
ZS     % Sign function. Gives 1 or -1 (no 0 in this case)
h      % Concatenate horizontally with previous 1
*      % Multiply. This gives plus or minus depending on increasing character
s      % Sum of vector. This realizes the additions or subtractions
       % Display implicitly
```
[Answer]
# CJam, 20
```
q~e`{~_W-g\:W@#*}%:+
```
[Try it online](http://cjam.aditsu.net/#code=q%7Ee%60%7B%7E_W-g%5C%3AW%40%23*%7D%25%3A%2B&input=%5B7637%20388%20389%20388%20387%2012%200%200%2034%2035%2035%2027%2027%202%5D)
**Explanation:**
```
q~       read and evaluate the input (array of numbers)
e`       RLE encode, obtaining [count number] pairs
{…}%     map each pair
  ~_     dump the count and number on the stack, and duplicate the number
  W-     subtract the previous number (W is initially -1 by default)
  g      get the sign of the result
  \      swap with the other copy of the number
  :W     store it in W (for next iteration)
  @#     bring the count to the top, and raise the number to that power
  *      multiply with the sign
:+       add all the results together
```
[Answer]
# JavaScript (ES6), 54
```
p=>eval(0+p.map(v=>x+='*-+'[(p>v)+2*(p<v)]+(p=v),x=1))
```
`eval` receives a comma separated list of expressions and returns the value of the last one.
**Test**
```
f=p=>eval(0+p.map(v=>x+='*-+'[(p>v)+2*(p<v)]+(p=v),x=1))
t=p=>(0+p.map(v=>x+='*-+'[(p>v)+2*(p<v)]+(p=v),x=1))
function Test() {
  var a=I.value.match(/\d+/g).map(x=>+x) // convert input to a numeric array
  
  var x=f(a),y=t(a)
  O.textContent='Value '+x+'\n(no eval '+y+')'
}  
Test()
```
```
#I { width:80%}
```
```
<input value='12, 0, 7, 7, 29, 10, 2, 2, 1' id=I>
<button onclick='Test()'>Test</button>
<pre id=O></pre>
```
[Answer]
# Julia, ~~76~~ 57 bytes
```
!x=[[" ""-*+"[2+sign(diff(x))]...] x]'|>join|>parse|>eval
```
My first time golfing Julia, so maybe there are obvious improvements.
[Try it online!](http://julia.tryitonline.net/#code=IXg9W1siICIiLSorIlsyK3NpZ24oZGlmZih4KSldLi4uXSB4XSd8PmpvaW58PnBhcnNlfD5ldmFsCgpwcmludCghWzEyLCAwLCA3LCA3LCAyOSwgMTAsIDIsIDIsIDFdKQ&input=)
Dennis saved a ton of bytes.
[Answer]
# Pyth - ~~23~~ ~~22~~ 20 bytes
As with Kenny's, multiplication doesn't work online.
```
vs.i+\+@L"*+-"._M-Vt
```
[Test Suite without doing eval](http://pyth.herokuapp.com/?code=s.i%2B%5C%2B%40L%22%2a%2B-%22._M-Vt&test_suite=1&test_suite_input=%5B0%5D%0A%5B1%5D%0A%5B78557%5D+%0A%5B0%2C0%5D%0A%5B1%2C1%5D%0A%5B2%2C2%5D%0A%5B0%2C1%5D%0A%5B1%2C0%5D%0A%5B1%2C2%5D%0A%5B2%2C1%5D%0A%5B15%2C4%2C4%5D%0A%5B9%2C8%2C1%5D%0A%5B4%2C2%2C2%2C4%5D%0A%5B10%2C9%2C9%2C12%5D%0A%5B1%2C1%2C2%2C2%2C3%2C3%5D%0A%5B5%2C5%2C4%2C4%2C3%2C3%5D%0A%5B3%2C1%2C4%2C1%2C5%2C9%2C2%2C6%2C5%2C3%2C5%2C9%5D%0A%5B7637%2C388%2C389%2C388%2C387%2C12%2C0%2C0%2C34%2C35%2C35%2C27%2C27%2C2%5D&debug=0).
[Answer]
# R, 92 bytes
There's likely still some good golfing that can be done here.
```
eval(parse(t=paste(i<-scan(),c(ifelse(d<-diff(i),ifelse(d>0,"+","-"),"*"),""),collapse="")))
```
Ungolfed:
```
i = scan()                # Read list from stdin
d = diff(i)               # Calculate difference between each element of list
s = ifelse(d,             # If d!=0
             ifelse(d>0,  # And if d>1
                    "+",  # Return plus
                    "-"), # Else return minus
             "*")         # Else (i.e. d==0) return multiply.
s = c(s,"")               # Pad the list s with an empty string, so it's the same
                          # length as i
p = paste(i,s,collapse="")# Paste the elements of i and s into one long string.
eval(parse(t=p))          # Evaluate the string as a language object.
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~34~~ 32 bytes
```
~b≜ḅ⟨h⟨h≡^⟩l⟩ᵐs₂ᶠ{zh>₁&ttṅ|tt}ᵐ+
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/vy7pUeechztaH81fkQHGnQvjHs1fmQPED7dOKH7U1PRw24Lqqgy7R02NaiUlD3e21pSU1AKltP//jzY3MzbXMbawAGJLKG2uY2ikYwCExiY6xqYgZGQORrH/owA "Brachylog – Try It Online")
[Answer]
# TI-BASIC, 146 bytes
I'll format it nicely when not on mobile. Sleep escapes me, so you get this. Enjoy.
```
Prompt L₁
"(→Str1
For(A,1,dim(L₁
{0,1→L₂
{0,L₁(A→L₃
LinReg(ax+b) L₁,L₃,Y₁
Equ►String(Y₁,Str2
sub(Str2,1,-1+inString(Str2,"X→Str2
If A>1
Then
L₁(A-1
2+(Ans>L₁(A))-(Ans<L₁(A
Str1+sub("+*-",Ans,1→Str1
End
Str1+Str2→Str2
End
expr(Str1
```
[Answer]
# Javascript ES6, ~~64~~ 62 chars
```
a=>eval(a.map((x,i)=>x+('*+-'[x<a[++i]|(x>a[i])*2])).join``+1)
```
[Answer]
# Java, 384 bytes
```
int s(int[]l){int n=l[0],m;for(int i=0;i<l.length-1;i++)if(l[i]<l[i+1])if(i<l.length-2&&l[i+1]!=l[i+2])n+=l[i+1];else{m=l[i+1];while(i<l.length-2&&l[i+1]==l[i+2])m*=l[(i++)+1];n+=m;}else if(l[i]>l[i+1])if(i<l.length-2&&l[i+1]!=l[i+2])n-=l[i+1];else{m=l[i+1];while(i<l.length-2&&l[i+1]==l[i+2])m*=l[(i++)+1];n-=m;}else{m=l[i];while(i<l.length-1&&l[i]==l[i+1])m*=l[i++];n+=m;}return n;}
```
**Ungolfed** [try online](http://ideone.com/tw9Gkw)
```
int s(int[] l)
{
    int n=l[0], m;
    for(int i=0; i<l.length-1; i++)
    {
        if(l[i] < l[i+1])
        {
            if (i<l.length-2 && l[i+1]!=l[i+2])
            {
                n += l[i+1];
            }
            else
            {
                m = l[i+1];
                while(i<l.length-2 && l[i+1]==l[i+2]) m *= l[(i++)+1];
                n += m;
            }
        }
        else if(l[i] > l[i+1])
        {
            if (i<l.length-2 && l[i+1]!=l[i+2])
            {
                n -= l[i+1];
            }
            else
            {
                m = l[i+1];
                while(i<l.length-2 && l[i+1]==l[i+2]) m *= l[(i++)+1];
                n -= m;
            }
        }
        else
        {
            m = l[i];
            while(i<l.length-1 && l[i]==l[i+1]) m *= l[i++];
            n += m;
        }
    }
    return n;
}
```
[Answer]
# Javascript ES6, 79 chars
```
a=>eval(`${a}`.replace(/(\d+),(?=(\d+))/g,(m,a,b)=>a+('*+-'[+a<+b|(+a>+b)*2])))
```
[Answer]
## Perl, 49 bytes
**48 bytes code + 1 for `-p`**
```
s/\d+ (?=(\d+))/$&.qw(* - +)[$&<=>$1]/ge;$_=eval
```
### Usage
```
perl -pe 's/\d+ (?=(\d+))/$&.qw(* - +)[$&<=>$1]/ge;$_=eval' <<< '12 0 7 7 29 10 2 2 1'
75
```
### Notes
I learnt here that you can capture a lookahead in PCRE, although it's a little unintuitive (`(?=(\d+))` instead of `((?=\d+))`). It does make sense after reading though as you would be capturing a zero-length match (the lookahead) with the latter, and instead capture the match with the former).
Thanks to [@ninjalj](https://codegolf.stackexchange.com/users/267/ninjalj) for saving 8 bytes!
[Answer]
## Actually, 30 bytes
```
;2@VpXdX`i-su"+*-"E`M' @o♀+εj≡
```
Unfortunately, because the eval (`≡`) command only evaluates literals on TIO, this program does not work on TIO.
Explanation:
```
;2@VpXdX`i-su"+*-"E`M' @o♀+εj≡
;                               duplicate input
 2@V                            overlapping sublists of length <= 2
    pXdX                        discard first and last element (length-1 sublists)
        `i-su"+*-"E`M           map: for each pair of elements
         i-su                     flatten, subtract, sign, increment (results in a 0 if b < a, 1 if b == a, and 2 if b > a)
             "+*-"E               select the correct operation
                     ' @o       put a space at the beginning of the list to pad it properly
                         ♀+     pairwise addition (since addition is not defined for strings and integers, this just zips the lists and flattens the result into a single flat list)
                           εj   join with empty string
                             ≡  eval
```
[Answer]
# [R](https://www.r-project.org/), ~~120~~ 44 bytes
```
r=rle(scan());c(1,sign(diff(r$v)))%*%r$v^r$l
```
[Try it online!](https://tio.run/##bY7NCsIwEITveYqBbSERD/npug3SVxGkGimUHlLw9eNWr/IxzCzMwNZGIcJDlJgRPKISQBA25EHekF7BkIwsgp8ZYjAGJSlHJ@lmUDGy7i/q6ZspZlOmVqe6Pu0@3zfr3HW24bwvr80@llJs7d7Ouf7Ua7jVbm3/Hmof "R – Try It Online")
The algorithm is similar to [this answer's](https://codegolf.stackexchange.com/a/82725/80010), but I only realized it after coding my answer. Much better than my original answer that was using `eval(parse)`.
Fully leverages R's vectorized operations - does the `*` operation first using `rle(x)$values ^ rle(x)$lenghts` and dot-products this vector with `sign( diff( rle(x)$values ) )` (predended with `1`).
[Answer]
# [05AB1E (legacy)](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~17~~ ~~16~~ 15 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ü.S…*-+sè‚ζJJ.E
```
-2 bytes thanks to *@Emigna*.
[Try it online](https://tio.run/##MzBNTDJM/f//8B694EcNy7R0tYsPr3jUMOvcNi8vPdf//6MNjXQMdMyB0MhSx9BAxwgIDWMB) or [verify all test cases](https://tio.run/##LY49DsIwDIV3TlF1hEcV54ckUxdAoitj1QEkTsAFOjEjRg6AOAETW3cO0YsEx61sy/b3bMvKnc50SRT2h21dFuPtUZT1cE/DtzqO/Wu5Xl2H99g/f5@mqXYJqSUNBc@mI0hBs1G3aBVHzj4453MPIaLxjBASMvFMyMHCchERRLRyLhO@HNlIT0eEGxjuHGRr7gxrlsPxsMaGs8l1fmRjPEwIHHHOHvK8guF1l1178e4P).
**Explanation:**
```
ü                  # Pair-wise loop over the (implicit) input-list
                   #  i.e. [12,0,7,7] → [[12,0],[0,7],[7,7]]
 .S                # Calculate the signum of the pair (-1 for a<b; 0 for a==b; 1 for a>b)
                   #  i.e. [[12,0],[0,7],[7,7]] → [1,-1,0]
   …*-+sè          # Index over "*-+" (with wrap-around)
                   #  i.e. [1,-1,0] → ['-','+','*']
         ‚ζ        # Zip the two lists together (appending the operands to the numbers)
                   #  i.e. [12,0,7,7] and ['-','+','*','+']
                   #   → [[12,'-'],[0,'+'],[7,'*'],[7,' ']]
           JJ      # Join them together
                   #  [[12,'-'],[0,'+'],[7,'*'],[7,' ']] → '12-0+7*7 '
             .E    # Evaluate as Python code
                   #  i.e. '12-0+7*7' → 61
```
[Answer]
# PHP, 103 bytes
Neat challenge. This got longer than expected. I think using `array_map` or similar won't improve the byte count, as anonymous functions are [still](https://wiki.php.net/rfc/arrow_functions) expensive in PHP.
```
foreach(fgetcsv(STDIN)as$v)(0<=$p?$o.=$p.('*-+'[($p>$v)+2*($p<$v)]):_)&&$p=$v;echo eval("return$o$v;");
```
Runs from command line, will prompt for a comma separated list, like:
```
php array_to_math.php
12, 0, 7, 7, 29, 10, 2, 2, 1
```
[Answer]
## PowerShell v2+, 62 bytes
```
-join($args|%{"$o"+'*+-'[($o-lt$_)+2*($o-gt$_)];$o=$_})+$o|iex
```
Takes input as space-separated command-line arguments, which gets converted into automatic array `$args`. We iterate through each element, using helper variable `$o` each iteration to remember what our previous entry was. We use a indexed-string to pull out the appropriate operator, done by performing math on the implicitly-converted Boolean values (e.g., if the previous entry is smaller, the `[]` evaluates to `1+2*0` so `'*+-'[1]` means the `+` is selected).
The concatenated strings are left on the pipeline. We collect all of those snippets together (e.g., `3-`, `1+`, `4-`, etc.) with a `-join` operation, concatenate on the final number (implicitly converted to string), and pipe it to `iex` (alias for `Invoke-Expression` and similar to `eval`).
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 25 bytes
Would like to cut it shorter, but I couldn't make an eval-less version work.
```
S+Uä!- ®"*+-"gZÎÃ¬)íU
OxU
```
[Try it online!](https://tio.run/##y0osKPn/P1g79PASRV2FQ@uUtLR1ldKjDvcdbj60RvPw2lAu/4rQ//@jzc2MzXWMLSyA2BJKm@sYGukYAKGxiY6xKQgZmYNRLAA "Japt – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) `-x`, ~~21~~ 19 bytes
```
änJ f mÎí*Uò¦ ®ÎpZÊ
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.6&code=5G5KIGYgbc7tKlXypiCuznBayg==&input=WzEyLDAsNyw3LDI5LDEwLDIsMiwxXQoteA==)
---
## Explanation
```
                        :Implicit input of array U
  J                     :Prepend -1
än                      :Get deltas
    f                   :Filter (remove 0s)
      m                 :Map
       Î                : Signs
        í               :Interleave
          U             :  Original input
           ò            :  Partition on
            ¦           :   Inequality
              ®         :  Map each sub-array Z
               Î        :    Get first element
                p       :    Raise to the power of
                 ZÊ     :     Length of Z
         *              :Reduce each pair of elements by multiplcation
                        :Implicitly reduce by addition and output
```
]