wvogel日記

自分用の技術備忘録が多めです.

IO

Haskellは、入出力は純粋な関数と区別されるという前回の気付きを踏まえ、数の大小を比べるコードを書いてみた。
って、これ、今思い出したけど、昔一回書いたことある.....
あのときはよく理解せずにどこかのサイトを参考に書いただけだったはず

main = do cs <- getContents
          putStr $ unlines $ map judge $  map read $ lines cs
judge :: Int -> String
judge n
    | n > 10  = "BIG"
    | n == 10 = "TEN"
    | otherwise = "SMALL"

実行結果は以下の通り

C:\ghc>110529
100
BIG
10
TEN
1
SMALL


なるほど


一部、関数を合成し短くした

main = do cs <- getContents
          putStr $ unlines $ map (judge.read) $ lines cs
judge :: Int -> String
judge n
    | n > 10  = "BIG"
    | n == 10 = "TEN"
    | otherwise = "SMALL"