ふつハス2日目

4章の練習問題の答えがなんだかすごいことに。8章をやらないとわからないなーw
今の理解の中で答えてみる。

行単位でソート

import System
import List

main = do cs <- getContents
          putStr $ sortLine cs
          
sortLine :: String -> String
sortLine cs = unlines $ sort $ lines cs

で、以下のファイルを食わせると

1
3
6
2
4
5

こうなる。

1
2
3
4
5
6

行単位ってこういうことでいいのかな?

同じ行が連続していたらそれを1行にまとめる

import System
import List

main = do cs <- getContents
          putStr $ uniq cs
          
uniq :: String -> String
uniq cs = unlines $ map head $ group $ lines cs

で、以下のファイルを食わせると

huge
hoge
hoge
fore

こうなる。

huge
hoge
fore

今日は時間がないので、5.6章は明日。