Anonymous user
Sorting algorithms/Bogosort: Difference between revisions
→{{header|Haskell}}
Line 864:
import Data.Array.IO
import Control.Monad
isSortedBy _ [] = True
isSortedBy f xs = all (uncurry f) . (zip <*> tail) $ xs
▲isSorted :: (Ord a) => [a] -> Bool
-- from http://www.haskell.org/haskellwiki/Random_shuffle
shuffle :: [a] -> IO [a]
Line 883 ⟶ 884:
newArray :: Int -> [a] -> IO (IOArray Int a)
newArray n xs = newListArray (1,n) xs
bogosortBy :: (a -> a -> Bool) -> [a] -> IO [a]
bogosortBy _ [] = return []
bogosortBy f xs | isSortedBy f xs = return xs
bogosort ::
bogosort
▲ | otherwise = shuffle li >>= bogosort</lang>
Example:
<pre>
|