Q:

First pair out of order

I would like to know how to efficiently determine the first pair of elements in a list that are not in order according to some predicate p.  That is, list[i] > list[j] according to p, and i is the smallest index for which this is true. It is easy, but gauche, using nested loops.

A:

Given this test list
list = {1,2,3,4,3,4,5,6}

we can extract the first pair of elements that are not increasing as follows:
Select[Partition[list, 2, 1], ! Less @@ # &, 1]

{{4,3}} 

For other predicates you can adjust the second argument of Select accordingly.

A:

Pattern-matching approach:
{1, 2, 3, 4, 3, 4, 5, 6} /. {a___, x_, y_, b___} /; x > y :> {Length[{a}] + 1, {x, y}}
(* {4, {4, 3}} *)

That means 4>3 at the 4th position in the list.

Another less elegant approach:
greaterQ = (Last@Take[list, #] > First@Drop[list, #] & /@ Range[Length@list - 1])~Append~False
(* {False, False, False, True, False, False, False, False} *)

p = Position[greaterQ, True]
(* {{4}} *)

list[[Flatten@{p, p + 1}]]
(* {4, 3} *)

A:

In V10 There's FirstCase:
FirstCase[Partition[list, 2, 1], {x_, y_} /; x > y]

Otherwise:
Cases[Partition[list, 2, 1], {x_, y_} /; x > y, 1, 1]

