Sorting algorithms/Bubble sort: Difference between revisions
→{{header|Haskell}}: Changed to use Maybe's monadic interface.
(Added Oz example.) |
Underscore (talk | contribs) (→{{header|Haskell}}: Changed to use Maybe's monadic interface.) |
||
Line 553:
| otherwise = x:(_bsort (x2:xs))
_bsort s = s</lang>
This version uses the polymorphic <tt>Maybe</tt> type to designate unchanged lists. (The type signature of <tt>_bsort</tt> is now <tt>Ord a => [a] -> Maybe [a]</tt>.) It is slightly faster than the previous one.
<lang haskell>bsort :: Ord a => [a] -> [a]▼
<lang haskell>import Data.Maybe (fromMaybe)
bsort s = case _bsort s of▼
import Control.Monad
where _bsort (x:x2:xs) | x > x2 = case _bsort (x:xs) of▼
then Just $ x2 : fromMaybe (x:xs) (_bsort $
else liftM (x:) $ _bsort
_bsort _ =
=={{header|J}}==
|