我写了一个sort
函数。这很奇怪。
sort :: (Eq a, Ord a) => [a] -> [a]
sort xs = repeat'' sort' (xs) (length xs)
repeat'' f xs 0 = xs
repeat'' f xs n = repeat'' f (f xs) (n-1)
sort' (x:y:xs)
| x < y = y : sort' (x:xs)
| otherwise = x : sort' (y:xs)
sort' x = x
我如何使用let-in
构造来美化它?
我认为我的尝试看起来并不好:
sort :: (Eq a, Ord a) => [a] -> [a]
sort xs = let
r f xs 0 = xs
r f xs n = r f (f xs) (n-1)
in r f' xs $ length xs where
f' (x:y:xs) | x < y = y : f' (x:xs)
| otherwise = x : f' (y:xs)
f' a = a