September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,26 @@
|
|||
import Data.List
|
||||
import Data.Function (on)
|
||||
|
||||
data Person =
|
||||
P String
|
||||
Int
|
||||
deriving (Eq)
|
||||
|
||||
instance Show Person where
|
||||
show (P name val) = "Person " ++ name ++ " with value " ++ show val
|
||||
|
||||
instance Ord Person where
|
||||
compare (P a _) (P b _) = compare a b
|
||||
|
||||
pVal :: Person -> Int
|
||||
pVal (P _ x) = x
|
||||
|
||||
people :: [Person]
|
||||
people = [P "Joe" 12, P "Bob" 8, P "Alice" 9, P "Harry" 2]
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
mapM_ print $ sort people
|
||||
putStrLn []
|
||||
mapM_ print $ sortBy (on compare pVal) people
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import Data.Ord (comparing)
|
||||
import Data.List (sortBy)
|
||||
|
||||
xs :: [(String, String, Int)]
|
||||
xs =
|
||||
zip3
|
||||
(words "Richard John Marvin Alan Maurice James")
|
||||
(words "Hamming McCarthy Minskey Perlis Wilkes Wilkinson")
|
||||
[1915, 1927, 1926, 1922, 1913, 1919]
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ sortBy (comparing (\(_, _, y) -> y)) xs
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
import List
|
||||
|
||||
data Person = P String Int deriving Eq
|
||||
instance Show Person where
|
||||
show (P name val) = "Person "++name++" with value "++(show val)
|
||||
instance Ord Person where
|
||||
compare (P n1 _) (P n2 _) = compare n1 n2
|
||||
|
||||
people = [(P "Joe" 12), (P "Bob" 8), (P "Alice" 9), (P "Harry" 2)]
|
||||
sortedPeople = sort people
|
||||
sortedPeopleByVal = sortBy (\(P _ v1) (P _ v2)->compare v1 v2) people
|
||||
Loading…
Add table
Add a link
Reference in a new issue