Weapons of Math Instruction
Javascript ES7 49 bytes
a=>a.reduce((c,d,e)=>[c**d,c+d,c-d,c*d,c/d][e%5])
Saved 9 bytes thanks to Dom Hastings, saved another 6 thanks to Leaky Nun
Uses the new exponentiation operator.
Haskell, 76 65 64 62 bytes
Thanks to @Damien for removing another two bytes=)
f(u:v)=foldl(\x(f,y)->f x y)u(zip(v>>[(+),(-),(*),(/),(**)])v)
This uses the >>
which here just appends the list [(+),...]
to itself length v
times. The rest still works still the same as the old versions.
Old versions:
These solutions make use of the infinite lists, as cycle[...]
just repeats the given list infinitely. Then it basically gets zip
ed with the list of numbers, and we just fold
(reduce in other languages) the zipped list via a lambda, that applies the operators to the accumulator/current list element.
f(u:v)=foldl(\x(f,y)->f x y)u(zip(cycle[(+),(-),(*),(/),(**)])v)
f(u:v)=foldl(\x(y,f)->f x y)u(zip v(cycle[(+),(-),(*),(/),(**)]))
f l=foldl(\x(y,f)->f x y)(head l)(zip(drop 1l)(cycle[(+),(-),(*),(/),(**)]))
Pyke, 22 21 bytes
lt5L%"+-*/^"L@\RJQ_XE
Try it here!
lt5L% - map(len(input)-1, %5)
"+-*/^"L@ - map(^, "+-*/^"[<])
\RJ - "R".join(^)
E - pyke_eval(^, V)
Q_X - splat(reversed(input))