Last active
January 10, 2016 01:43
-
-
Save mitsuji/27ed911d11c00016abe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Maybe (isNothing,fromJust) | |
type ID = Int | |
type Rank = Int | |
type Name = String | |
type RankDB = [(Rank,ID)] | |
type NameDB = [(ID,Name)] | |
idFromRank :: RankDB -> Rank -> Maybe ID | |
idFromRank db rk = lookup rk db | |
nameFromId :: NameDB -> ID -> Maybe Name | |
nameFromId db id = lookup id db | |
topThree :: RankDB -> NameDB -> Maybe (Name,Name,Name) | |
topThree rdb ndb = | |
let maybeId1 = idFromRank rdb 1 | |
in | |
if isNothing maybeId1 then Nothing | |
else | |
let maybeName1 = nameFromId ndb $ fromJust maybeId1 | |
in | |
if isNothing maybeName1 then Nothing | |
else | |
let maybeId2 = idFromRank rdb 2 | |
in | |
if isNothing maybeId2 then Nothing | |
else | |
let maybeName2 = nameFromId ndb $ fromJust maybeId2 | |
in | |
if isNothing maybeName2 then Nothing | |
else | |
let maybeId3 = idFromRank rdb 3 | |
in | |
if isNothing maybeId3 then Nothing | |
else | |
let maybeName3 = nameFromId ndb $ fromJust maybeId3 | |
in | |
if isNothing maybeName3 then Nothing | |
else Just ( | |
fromJust maybeName1, | |
fromJust maybeName2, | |
fromJust maybeName3 | |
) | |
topThreeC :: RankDB -> NameDB -> Maybe (Name,Name,Name) | |
topThreeC rdb ndb = | |
case idFromRank rdb 1 of | |
Nothing -> Nothing | |
Just id1 -> case nameFromId ndb id1 of | |
Nothing -> Nothing | |
Just n1 -> case idFromRank rdb 2 of | |
Nothing -> Nothing | |
Just id2 -> case nameFromId ndb id2 of | |
Nothing -> Nothing | |
Just n2 -> case idFromRank rdb 3 of | |
Nothing -> Nothing | |
Just id3 -> case nameFromId ndb id3 of | |
Nothing -> Nothing | |
Just n3 -> Just (n1,n2,n3) | |
topThreeM :: RankDB -> NameDB -> Maybe (Name,Name,Name) | |
topThreeM rdb ndb = do | |
id <- idFromRank rdb 1 | |
n1 <- nameFromId ndb id | |
id <- idFromRank rdb 2 | |
n2 <- nameFromId ndb id | |
id <- idFromRank rdb 3 | |
n3 <- nameFromId ndb id | |
return (n1,n2,n3) | |
main = do | |
print $ topThree [(1,101),(2,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThree [(1,101),(4,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThree [(1,101),(2,102),(3,103)] [(101,"1st"),(104,"4th"),(103,"3rd")] | |
print $ topThreeC [(1,101),(2,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThreeC [(1,101),(4,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThreeC [(1,101),(2,102),(3,103)] [(101,"1st"),(104,"4th"),(103,"3rd")] | |
print $ topThreeM [(1,101),(2,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThreeM [(1,101),(4,102),(3,103)] [(101,"1st"),(102,"2nd"),(103,"3rd")] | |
print $ topThreeM [(1,101),(2,102),(3,103)] [(101,"1st"),(104,"4th"),(103,"3rd")] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment