2013年10月20日日曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)12章(新しいクラスのオブジェクト), 12.1(Timeクラス)、12.2(練習問題)、ハッピーバースデーをHaskellで解いてみる。

その他参考書籍

ハッピーバースデー

コード(BBEdit)

sample.hs

{--
{-# OPTIONS -Wall -Werror #-}
--}

import System.Time

main :: IO ()
main = do
    putStrLn "生まれた"
    putStrLn "年"
    year <- getLine
    putStrLn "月"
    month <- getLine
    putStrLn "日"
    day <- getLine
    now <- getClockTime
    nowCal <- toCalendarTime now
    let a = months !! (read month :: Int)
        b = CalendarTime {ctYear = read year :: Int, 
                          ctMonth = a,
                          ctDay = read day :: Int}
        c = getAge b nowCal
    putStrLn $ "年齢: " ++ (show c) ++ "歳"
    mapM_ putStrLn $ replicate c "平手打ち(パシッ)"

getAge :: CalendarTime -> CalendarTime -> Int
getAge a b = 
    let c = ctYear a
        d = ctMonth a
        e = ctDay a
        f = ctYear b
        g = ctMonth b
        h = ctDay b
        i = f - c
        j | d < g = i - 1
          | d == g && e < h = i - 1
          | otherwise = i
    in j

months :: [Month]
months = [undefined, January, February, March, April, May, June, July, August,
          September, October, November, December]

入出力結果(Terminal, runghc)

$ ghc --make sample.hs
[1 of 1] Compiling Main             ( sample.hs, sample.o )

sample.hs:19:13: Warning:
    Fields of `CalendarTime' not initialised: ctHour, ctMin, ctSec,
                                              ctPicosec, ctWDay, ctYDay, ctTZName, ctTZ, ctIsDST
    In the expression:
      CalendarTime
        {ctYear = (read year :: Int), ctMonth = a,
         ctDay = (read day :: Int)}
    In an equation for `b':
        b = CalendarTime
              {ctYear = (read year :: Int), ctMonth = a,
               ctDay = (read day :: Int)}
    In the expression:
      do { putStrLn "\29983\12414\12428\12383";
           putStrLn "\24180";
           year <- getLine;
           putStrLn "\26376";
           .... }
Linking sample ...
$ ./sample
生まれた
年
2000
月
10
日
19
年齢: 12歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
10
日
20
年齢: 13歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
10
日
21
年齢: 13歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
11
日
19
年齢: 13歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
11
日
21
年齢: 13歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
9
日
19
年齢: 12歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$ ./sample
生まれた
年
2000
月
9
日
21
年齢: 12歳
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
平手打ち(パシッ)
$

0 コメント:

コメントを投稿