blob: 8da89471c097876ef91b7beef6d430a70086ec1f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
module Ch2 where
import Data.List
digits :: Integral a => a -> [a]
digits 0 = []
digits x = digits (div x 10) ++ [mod x 10]
helper :: Integral a => a -> a -> a
helper acc n
| n < 10 = acc
| otherwise =
helper (acc+1) (product $ digits n)
persistenceSort :: Integral a => [a] -> [a]
persistenceSort xs =
sortBy (\a b -> let ha = helper 0 a; hb = helper 0 b in
if ha /= hb then
compare ha hb
else compare a b) xs
main :: IO ()
main = do
print $ persistenceSort [15,99,1,34]
print $ persistenceSort [50,25,33,22]
|