Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
(Added Oz example.) |
Underscore (talk | contribs) (→{{header|Haskell}}: Changed to use Maybe's monadic interface.) |
||
Line 553: | Line 553: | ||
| otherwise = x:(_bsort (x2:xs)) |
| otherwise = x:(_bsort (x2:xs)) |
||
_bsort s = s</lang> |
_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. |
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>import Data.Maybe (fromMaybe) |
|||
⚫ | |||
import Control.Monad |
|||
Nothing -> s |
|||
Just s2 -> bsort s2 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Nothing -> Just $ x2:x:xs |
|||
⚫ | |||
Just xs2 -> Just $ x2:xs2 |
|||
then Just $ x2 : fromMaybe (x:xs) (_bsort $ x:xs) |
|||
else liftM (x:) $ _bsort (x2:xs) |
|||
_bsort _ = Nothing</lang> |
|||
_bsort _ = Nothing</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |