計算機科学のブログ

関数型プログラミングの基礎 ファーストクラス関数 compare関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT1(関数型プログラミングの基礎)、LESSON 4(ファーストクラス関数)、4.4(練習問題)Q4-1の解答を求めてみる。

コード

lesson/app/Main.hs

module Main where

import Data.List (sortBy)

-- import Lib ()

compareLastName :: (Ord a1, Ord a2) => (a2, a1) -> (a2, a1) -> Ordering
compareLastName name1 name2 =
  let c = compare (snd name1) (snd name2)
   in if c == EQ
        then compare (fst name1) (fst name2)
        else c

-- まだ出てきてない構文を利用してよりlet句(where句)簡潔に関数を記述
compareLastName1 :: (Ord a1, Ord a2) => (a2, a1) -> (a2, a1) -> Ordering
compareLastName1 (firstName1, lastName1) (firstName2, lastName2) =
  let c = compare lastName1 lastName2
   in if c == EQ
        then compare firstName1 firstName2
        else c

names :: [(String, String)]
names =
  [ ("Ian", "Curtis"),
    ("Bernard", "Sumner"),
    ("Peter", "Hook"),
    ("Stephen", "Morris"),
    ("Ian", "Sumner"),
    ("Bernard", "Sumner")
  ]

main :: IO ()
main = do
  mapM_ print names
  mapM_
    ( \(fname, f) -> do
        print fname
        mapM_ print $ sortBy f names
    )
    [ ("compareLastName", compareLastName),
      ("compareLastName1", compareLastName1)
    ]

入出力結果(Terminal, Zsh)

% stack runghc app/Main.hs
("Ian","Curtis")
("Bernard","Sumner")
("Peter","Hook")
("Stephen","Morris")
("Ian","Sumner")
("Bernard","Sumner")
"compareLastName"
("Ian","Curtis")
("Peter","Hook")
("Stephen","Morris")
("Bernard","Sumner")
("Bernard","Sumner")
("Ian","Sumner")
"compareLastName1"
("Ian","Curtis")
("Peter","Hook")
("Stephen","Morris")
("Bernard","Sumner")
("Bernard","Sumner")
("Ian","Sumner")
%