Longest non-decreasing subsequence, in Haskell
A few general remarks

I find that stepped whiles are as evil as nested if statements for readability of the code. Here, you have three levels of nesting.
Relying on the surrounding environment for variables can some times obscure possibility to generalize (You make use of x and m for inner definitions)

So here is what I would do

import Data.Sequence as DS
import Numeric.Search.Range as R

seqLast::Seq a -&gt; a
seqLast xs = index xs ((DS.length xs) - 1)

insert_idx :: Ord a =&gt; a -&gt; Seq [a] -&gt; Maybe Int
insert_idx y m = searchFromTo (fn y) 0 $ (DS.length m) - 1
  where fn y idx = y &lt; (head (index m idx))

-- Return the longest non-decreasing subsequence of an input sequence of integers
nonDecreasing::[Int] -&gt; [Int]
nonDecreasing = Prelude.reverse . seqLast . makeM
  where makeM = foldl updateM empty
        updateM m x | DS.null m = singleton [x]
                    | otherwise = case insert_idx x m of
                        Nothing -&gt; m |&gt; (x: seqLast m)
                        Just l -&gt; update l (x:geti l m) m
        geti 0 _ = []
        geti l m = index m (l -1)