リストの基本処理3
tailコマンドはheadコマンドの逆で入力の最後の数行を表示します
tail.hs
main = do cs <- getContents putStr $ lastNLines 10 cs lastNLines n cs = unlines $ takeLast n $ lines cs takeLast n ss = reverse $ take n $ reverse ss
csもssもファイル名です
ファイルには命名規則があるみたいなのですが、そこはとりあえず気にしない★
reverse関数:リストを逆順にします
reverse [ 1, 2, 3 ] → [ 3, 2, 1 ] reverse [ ] → [ ] reverse "string " → "gnirts" reverse " " → " " reverse ["abc","def","gfi" ] → [ "gfi","def","abc" ]
tail.hs解説
ssリストを逆順にして先頭のnの要素を抽出して再度逆順にする
csリストを分割してからnの要素を文字列の行に連結します
最後に標準入力で末尾10行を出力します
理解はできるけど、ここ微妙。。。。
再復習しよう。。。。