Mercurial > hg > Members > atton > delta_monad
changeset 46:cb5c190aa45d
Define bubble sort
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 11 Nov 2014 13:28:55 +0900 |
parents | 0c2d758406b1 |
children | 1aefea69f71b |
files | delta.hs |
diffstat | 1 files changed, 13 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/delta.hs Sun Nov 09 12:04:20 2014 +0900 +++ b/delta.hs Tue Nov 11 13:28:55 2014 +0900 @@ -23,6 +23,10 @@ instance Functor Delta where fmap f (Delta xs x ys y) = Delta xs (f x) ys (f y) +-- not proof +fmapS :: (Show a) => (a -> b) -> Delta a -> Delta b +fmapS f (Delta lx x ly y) = Delta (lx ++ [(show x)]) (f x) (ly ++ [(show y)]) (f y) + instance Applicative Delta where pure f = Delta [] f [] f (Delta lf f lg g) <*> (Delta lx x ly y) = Delta (lf ++ lx) (f x) (lg ++ ly) (g y) @@ -58,3 +62,12 @@ primeCount :: Int -> Delta Int primeCount x = generator x >>= primeFilter >>= count + +bubbleSort :: [Int] -> Delta [Int] +bubbleSort [] = returnS [] +bubbleSort xs = fmapS (\x -> (replicate maxNumCount maxNum) ++ x) (bubbleSort remainList) + where + maxNum = maximum xs + remainList = filter (/= maxNum) xs + maxNumCount = (length xs) - (length remainList) +