# HG changeset patch # User Yasutaka Higa # Date 1415680135 -32400 # Node ID cb5c190aa45d9b887a3c70c847ed70f0ea937df6 # Parent 0c2d758406b177c7b84326044b69aec4f40fa86c Define bubble sort diff -r 0c2d758406b1 -r cb5c190aa45d delta.hs --- 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) +