{"id":160,"date":"2016-01-10T11:10:44","date_gmt":"2016-01-10T02:10:44","guid":{"rendered":"http:\/\/mitsuji.org\/?p=160"},"modified":"2016-01-10T11:11:23","modified_gmt":"2016-01-10T02:11:23","slug":"either%e3%83%a2%e3%83%8a%e3%83%89%e3%81%ae%e4%bd%bf%e3%81%84%e9%81%93-%e9%83%a8%e5%88%86%e7%9a%84%e3%81%aa%e5%a4%b1%e6%95%97%e3%82%92%e5%85%a8%e4%bd%93%e7%9a%84%e3%81%aa%e5%a4%b1%e6%95%97%e3%81%a8","status":"publish","type":"post","link":"https:\/\/mitsuji.org\/?p=160","title":{"rendered":"Either\u30e2\u30ca\u30c9\u306e\u4f7f\u3044\u9053 \u90e8\u5206\u7684\u306a\u5931\u6557\u3092\u5168\u4f53\u7684\u306a\u5931\u6557\u3068\u3059\u308b\u8a08\u7b97(2)"},"content":{"rendered":"<p>\u203b\u3053\u306e\u8a18\u4e8b\u306eMaybe\u7de8\u306f<a href=\"\/?p=152\">\u3053\u3061\u3089<\/a><\/p>\n<p>id\u3068\u540d\u524d\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u3001\u9806\u4f4d\u3068id\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u306a\u308b\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u8003\u3048\u3066\u307f\u3088\u3046\u3002<\/p>\n<p>2\u4f4d\u306e\u540d\u524d\u3092\u53d6\u308a\u51fa\u3059\u306b\u306f\u3001idFromRank \u3067 2\u4f4d\u306eid\u3092\u53d6\u308a\u51fa\u3057\u3001\u305d\u306e id \u3092 nameFromId \u306b\u6e21\u3057\u3066\u540d\u524d\u3092\u5f97\u308b\u3082\u306e\u3068\u3059\u308b\u3002<br \/>\nidFromRank \u3082 nameFromId \u3082\u8981\u6c42\u3055\u308c\u305f\u9806\u4f4d\u3084id\u306e\u30c7\u30fc\u30bf\u304c\u306a\u3051\u308c\u3070 Left \u306b\u8a55\u4fa1\u3055\u308c\u308b\u3082\u306e\u3068\u3059\u308b\u3002<\/p>\n<pre class=\"lang:haskell\">\n\ntype Err = String\ntype ID = Int\ntype Rank = Int\ntype Name = String\ntype RankDB = [(Rank,ID)]\ntype NameDB = [(ID,Name)]\n\nidFromRank :: RankDB -> Rank -> Either Err ID\nidFromRank db rk =\n  case lookup rk db of\n    Nothing -> Left $ \"id of rank \" ++ (show rk) ++ \" was not found.\"\n    Just id -> Right id\n\nnameFromId :: NameDB -> ID -> Either Err Name\nnameFromId db id =\n  case lookup id db of\n    Nothing   -> Left $ \"name of id \" ++ (show id) ++ \" was not found.\"\n    Just name -> Right name\n\n<\/pre>\n<p>\u3053\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u3063\u3066\u3001\u4e0a\u4f4d3\u4f4d\u306e\u540d\u524d\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570\u3092\u4f5c\u6210\u3057\u3066\u307f\u3088\u3046\u3002<br \/>\n\u30dd\u30a4\u30f3\u30c8\u306f\u3001idFromRank \u3082 nameFromId \u3082 Left \u306b\u8a55\u4fa1\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u95a2\u6570\u3082Either\u578b\u306b\u8a55\u4fa1\u3055\u308c\u308b\u3082\u306e\u3068\u3057\u30011\u4f4d\u304b\u30893\u4f4d\u307e\u3067\u306eid\u304c\u3059\u3079\u3066\u5b58\u5728\u3057\u3001\u305d\u308c\u3089\u306e\u540d\u524d\u304c\u3059\u3079\u3066\u5b58\u5728\u3059\u308b\u5834\u5408\u306e\u307fRight\u306b\u8a55\u4fa1\u3055\u308c\u3001\u305d\u308c\u4ee5\u5916\u306e\u3068\u304d\u306fLeft\u306b\u8a55\u4fa1\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3060\u3002<\/p>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308c\u3070\u3088\u3044\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"lang:haskell\">\nmain = do\n  print $ topThree [(1,101),(2,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThree [(1,101),(4,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThree [(1,101),(2,102),(3,103)] [(101,\"1st\"),(104,\"4th\"),(103,\"3rd\")]\n<\/pre>\n<p>\u5f0f\u3092\u4f7f\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u7d20\u76f4\u306b\u8a18\u8ff0\u3059\u308b\u5834\u5408\u3001\u7b46\u8005\u306e\u80fd\u529b\u3067\u306f\u4e0b\u8a18\u306e\u8a18\u8ff0\u304c\u9650\u754c\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3069\u3046\u8003\u3048\u3066\u3082\u5730\u7344\u3067\u3042\u308b\u3002\u6211\u3005\u304c\u95a2\u6570\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u6c42\u3081\u3066\u3044\u305f\u3082\u306e\u306f\u3053\u3093\u306a\u3082\u306e\u3067\u306f\u306a\u304b\u3063\u305f\u306f\u305a\u3060\u3002<\/p>\n<pre class=\"lang:haskell\">\ntopThree :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThree rdb ndb =\n  let eitherId1 = idFromRank rdb 1\n  in\n   if isLeft eitherId1 then Left $ fromLeft eitherId1\n   else\n     let eitherName1 = nameFromId ndb $ fromRight eitherId1\n     in\n      if isLeft eitherName1 then Left $ fromLeft eitherName1\n      else\n        let eitherId2 = idFromRank rdb 2\n        in\n         if isLeft eitherId2 then Left $ fromLeft eitherId2\n         else\n           let eitherName2 = nameFromId ndb $ fromRight eitherId2\n           in\n            if isLeft eitherName2 then Left $ fromLeft eitherName2\n            else\n              let eitherId3 = idFromRank rdb 3\n              in\n               if isLeft eitherId3 then Left $ fromLeft eitherId3\n               else\n                 let eitherName3 = nameFromId ndb $ fromRight eitherId3\n                 in\n                  if isLeft eitherName3 then Left $ fromLeft eitherName3\n                  else Right (\n                    fromRight eitherName1,\n                    fromRight eitherName2,\n                    fromRight eitherName3\n                    )\n  where\n    fromLeft (Left x) = x \n    fromRight (Right x) = x\n\n<\/pre>\n<p>Haskell \u306b\u306f case \u6587\u304c\u3042\u308b\u306e\u3067\u3001\u52b9\u679c\u7684\u306b\u4f7f\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6539\u5584\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u3057\u304b\u3057 case \u6587\u306e\u5165\u308c\u5b50\u3082\u672c\u8cea\u7684\u306b\u306f\u5730\u7344\u3067\u3042\u308b\u3002\u3053\u3093\u306a\u3053\u3068\u306a\u3089\u624b\u7d9a\u304d\u578b\u8a00\u8a9e\u3092\u4f7f\u3048\u3070\u3088\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3002<\/p>\n<pre class=\"lang:haskell\">\ntopThreeC :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThreeC rdb ndb =\n  case idFromRank rdb 1 of\n    Left e -> Left e\n    Right id1 -> case nameFromId ndb id1 of\n      Left e -> Left e\n      Right n1 -> case idFromRank rdb 2 of\n        Left e -> Left e\n        Right id2 -> case nameFromId ndb id2 of\n          Left e -> Left e\n          Right n2 -> case idFromRank rdb 3 of\n            Left e -> Left e\n            Right id3 -> case nameFromId ndb id3 of\n              Left e -> Left e\n              Right n3 -> Right (n1,n2,n3)\n\n<\/pre>\n<p>\u305d\u3053\u3067 Either\u30e2\u30ca\u30c9\u767b\u5834\u3002<br \/>\nEither\u578b\u306e\u30e2\u30ca\u30c9\u3068\u3057\u3066\u306e\u6027\u8cea\u3092\u4f7f\u3046\u3068\u3001\u4e0a\u8a18\u3068\u307e\u3063\u305f\u304f\u540c\u3058\u610f\u5473\u306e\u95a2\u6570\u3092\u4e0b\u8a18\u306e\u5185\u5bb9\u3060\u3051\u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u30e2\u30ca\u30c9\u3068\u3057\u3066\u306eEither\u578b\u306b\u306f\u3001\u5931\u6557\u3059\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u8a08\u7b97\u3069\u3046\u3057\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3068\u304d\u306f\u3001\u4e00\u90e8\u3067\u3082\u5931\u6557\u3057\u305f\u3089\u3059\u3079\u3066\u304c\u5931\u6557\u3057\u305f\u3053\u3068\u306b\u3059\u308b<br \/>\n\u3068\u3044\u3046\u6027\u8cea\u304c\u6700\u521d\u304b\u3089\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"lang:haskell\">\ntopThreeM :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThreeM rdb ndb = do\n\n  id <- idFromRank rdb 1\n  n1 <- nameFromId ndb id\n\n  id <- idFromRank rdb 2\n  n2 <- nameFromId ndb id\n    \n  id <- idFromRank rdb 3\n  n3 <- nameFromId ndb id\n\n  return (n1,n2,n3)\n\n<\/pre>\n<p>\u5168\u90e8\u306e\u305b\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"lang:haskell\">\nimport Data.Either (isLeft)\n\ntype Err = String\ntype ID = Int\ntype Rank = Int\ntype Name = String\ntype RankDB = [(Rank,ID)]\ntype NameDB = [(ID,Name)]\n\nidFromRank :: RankDB -> Rank -> Either Err ID\nidFromRank db rk =\n  case lookup rk db of\n    Nothing -> Left $ \"id of rank \" ++ (show rk) ++ \" was not found.\"\n    Just id -> Right id\n\nnameFromId :: NameDB -> ID -> Either Err Name\nnameFromId db id =\n  case lookup id db of\n    Nothing   -> Left $ \"name of id \" ++ (show id) ++ \" was not found.\"\n    Just name -> Right name\n\n\n\n\ntopThree :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThree rdb ndb =\n  let eitherId1 = idFromRank rdb 1\n  in\n   if isLeft eitherId1 then Left $ fromLeft eitherId1\n   else\n     let eitherName1 = nameFromId ndb $ fromRight eitherId1\n     in\n      if isLeft eitherName1 then Left $ fromLeft eitherName1\n      else\n        let eitherId2 = idFromRank rdb 2\n        in\n         if isLeft eitherId2 then Left $ fromLeft eitherId2\n         else\n           let eitherName2 = nameFromId ndb $ fromRight eitherId2\n           in\n            if isLeft eitherName2 then Left $ fromLeft eitherName2\n            else\n              let eitherId3 = idFromRank rdb 3\n              in\n               if isLeft eitherId3 then Left $ fromLeft eitherId3\n               else\n                 let eitherName3 = nameFromId ndb $ fromRight eitherId3\n                 in\n                  if isLeft eitherName3 then Left $ fromLeft eitherName3\n                  else Right (\n                    fromRight eitherName1,\n                    fromRight eitherName2,\n                    fromRight eitherName3\n                    )\n  where\n    fromLeft (Left x) = x \n    fromRight (Right x) = x\n\n\n\ntopThreeC :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThreeC rdb ndb =\n  case idFromRank rdb 1 of\n    Left e -> Left e\n    Right id1 -> case nameFromId ndb id1 of\n      Left e -> Left e\n      Right n1 -> case idFromRank rdb 2 of\n        Left e -> Left e\n        Right id2 -> case nameFromId ndb id2 of\n          Left e -> Left e\n          Right n2 -> case idFromRank rdb 3 of\n            Left e -> Left e\n            Right id3 -> case nameFromId ndb id3 of\n              Left e -> Left e\n              Right n3 -> Right (n1,n2,n3)\n\n\n\ntopThreeM :: RankDB -> NameDB -> Either Err (Name,Name,Name)\ntopThreeM rdb ndb = do\n\n  id <- idFromRank rdb 1\n  n1 <- nameFromId ndb id\n\n  id <- idFromRank rdb 2\n  n2 <- nameFromId ndb id\n    \n  id <- idFromRank rdb 3\n  n3 <- nameFromId ndb id\n\n  return (n1,n2,n3)\n\n\n\n\nmain = do\n  print $ topThree [(1,101),(2,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThree [(1,101),(4,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThree [(1,101),(2,102),(3,103)] [(101,\"1st\"),(104,\"4th\"),(103,\"3rd\")]\n  print $ topThreeC [(1,101),(2,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThreeC [(1,101),(4,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThreeC [(1,101),(2,102),(3,103)] [(101,\"1st\"),(104,\"4th\"),(103,\"3rd\")]\n  print $ topThreeM [(1,101),(2,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThreeM [(1,101),(4,102),(3,103)] [(101,\"1st\"),(102,\"2nd\"),(103,\"3rd\")]\n  print $ topThreeM [(1,101),(2,102),(3,103)] [(101,\"1st\"),(104,\"4th\"),(103,\"3rd\")]\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u203b\u3053\u306e\u8a18\u4e8b\u306eMaybe\u7de8\u306f\u3053\u3061\u3089 id\u3068\u540d\u524d\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u3001\u9806\u4f4d\u3068id\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u306a\u308b\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u8003\u3048\u3066\u307f\u3088\u3046\u3002 2\u4f4d\u306e\u540d\u524d\u3092\u53d6\u308a\u51fa\u3059\u306b\u306f\u3001idFromRank \u3067 2\u4f4d\u306eid\u3092\u53d6\u308a\u51fa\u3057\u3001\u305d\u306e  &hellip; <a href=\"https:\/\/mitsuji.org\/?p=160\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Either\u30e2\u30ca\u30c9\u306e\u4f7f\u3044\u9053 \u90e8\u5206\u7684\u306a\u5931\u6557\u3092\u5168\u4f53\u7684\u306a\u5931\u6557\u3068\u3059\u308b\u8a08\u7b97(2)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[3,5],"_links":{"self":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":1,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}