開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Haskell (プログラミング言語)
- Haskell Platform(ghci)(処理系)
関数プログラミング入門(Richard Bird (著)、山下伸夫 (翻訳)、オーム社)の第2章(単純なデータ型)、2.7(文字列)、練習問題2.7.1、2.7.2、2.7.3を取り組んでみる。
練習問題2.7.1、2.7.2、2.7.3
コード(Emacs)
s1, s2, s3 :: String s1 = show (show 42) s2 = show 42 ++ show 42 s3 = show "\n" monthName :: Integer -> String monthName n | n == 1 = "January" | n == 2 = "Feburary" | n == 3 = "March" | n == 4 = "April" | n == 5 = "May" | n == 6 = "June" | n == 7 = "July" | n == 8 = "August" | n == 9 = "Semptember" | n == 10 = "October" | n == 11 = "November" | n == 12 = "December" | otherwise = error "Invalid" showDate :: (Integer, Integer, Integer) -> String showDate (d, m, y) = show d ++ " " ++ (monthName m) ++ " " ++ show y showDate' :: (Integer, Integer, Integer) -> String showDate' (d, m, y) = show d ++ s ++ " " ++ (monthName m) ++ " " ++ show y where r = mod d 10 s | r == 1 = "st" | r == 2 = "nd" | r == 3 = "rd" | otherwise = "th" main :: IO () main = do -- 2.7.1 putStrLn ("MacMillan" ++ "\n" ++ "Macmillan" ++ "\n" ++ "McMillan") -- 2.7.2 putStrLn (show (s1 == "\"42\"")) putStrLn (show (s2 == "4242")) putStrLn (show (s3 == "\"\\n\"")) -- 2.7.3 putStrLn (showDate (10, 12, 1997)) putStrLn (showDate' (10, 12, 1997)) putStrLn (showDate' (31, 12, 1997))
入出力結果(Terminal, ghci)
$ ghci sample7.hs GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( sample7.hs, interpreted ) Ok, modules loaded: Main. *Main> main MacMillan Macmillan McMillan True True True 10 December 1997 10th December 1997 31st December 1997 *Main> :q Leaving GHCi. $
0 コメント:
コメントを投稿