Finding Local Extremes Write a function or program that takes in a list and produces a list of the local extremes.

In a list [x_0, x_1, x_2...] a local extreme is an x_i such that x_(i-1) &lt; x_i and x_(i+1) &lt; x_i or x_(i-1) &gt; x_i and x_(i+1) &gt; x_i. Notice that the first and last elements of the list can never be local extremes.

So for some examples

local_extremes([1, 2, 1]) = [2]
local_extremes([0, 1, 0, 1, 0]) = [1, 0, 1]
local_extremems([]) = []


This is code golf so the shortest code wins!
J - 19 char

Couldn't help it ;)

(}:#~0,0&gt;2*/\2-/\])


Explanation follows:


2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the difference.
2*/\ - Now over each pair of the new list, take the product.
0&gt; - Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.
0, - Declare that the first element isn't an extreme element.
}: - Cut off the last element, because that can't possibly be an extreme either.
#~ - Use the true values on the right side to pick items from the list on the left side.


Usage:

   (}:#~0,0&gt;2*/\2-/\]) 1 2 1
2
   (}:#~0,0&gt;2*/\2-/\]) 0 1 0 1 0
1 0 1
   (}:#~0,0&gt;2*/\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}:#~0,0&gt;2*/\2-/\]) 3 4 4 4 2 5
2