September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,9 +1,26 @@
|
|||
import List
|
||||
import Char
|
||||
import Data.List (sortBy)
|
||||
import Data.Function (on)
|
||||
import Data.Char (toLower)
|
||||
|
||||
mycmp s1 s2 = case compare (length s2) (length s1) of
|
||||
EQ -> compare (map toLower s1) (map toLower s2)
|
||||
x -> x
|
||||
lengthThenAZ :: String -> String -> Ordering
|
||||
lengthThenAZ a b
|
||||
| d == EQ = on compare (toLower <$>) a b
|
||||
| otherwise = d
|
||||
where
|
||||
d = on compare length a b
|
||||
|
||||
strings = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
|
||||
sorted = sortBy mycmp strings
|
||||
descLengthThenAZ :: String -> String -> Ordering
|
||||
descLengthThenAZ a b
|
||||
| d == EQ = on compare (toLower <$>) a b
|
||||
| otherwise = d
|
||||
where
|
||||
d = on (flip compare) length a b
|
||||
|
||||
xs :: [String]
|
||||
xs = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
[unlines $ sortBy lengthThenAZ xs, unlines $ sortBy descLengthThenAZ xs]
|
||||
|
|
|
|||
|
|
@ -1,3 +1,26 @@
|
|||
import Data.List (sortBy)
|
||||
import Data.Char (toLower)
|
||||
import Data.Ord (comparing)
|
||||
import Data.Monoid
|
||||
mycmp s1 s2 = mappend (compare (length s2) (length s1))
|
||||
(compare (map toLower s1) (map toLower s2))
|
||||
|
||||
-- the Ordering instance of mappend is defined to yield
|
||||
-- lexicographical ordering.
|
||||
-- instance Monoid Ordering where
|
||||
-- mempty = EQ
|
||||
-- LT `mappend` _ = LT
|
||||
-- EQ `mappend` y = y
|
||||
-- GT `mappend` _ = GT
|
||||
|
||||
xs :: [String]
|
||||
xs = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
|
||||
|
||||
lowerCase :: String -> String
|
||||
lowerCase = (toLower <$>)
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ putStrLn $
|
||||
(unlines . flip sortBy xs) <$>
|
||||
[ comparing length <> comparing lowerCase -- Ascending length
|
||||
, flip (comparing length) <> comparing lowerCase -- Descending length
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue