2014年1月3日金曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の22章(まとめのプログラミング)、22.11(プログラミング実習)、実習22-1.をHaskellで解いてみる。

その他参考書籍

実習22-1.

コード(BBEdit)

Sample.hs

{-# OPTIONS -Wall -Werror #-}
module Main where

import System.Environment

main :: IO ()
main = do
    (name:_) <- getArgs
    contents <- readFile name
    mapM_ putStrLn $ doubled contents

doubled :: String -> [String]
doubled s =
    let xs = words s
    in foldr (\(a, b) acc -> if a == b then
                                 a:acc
                             else
                                 acc) [] $ zip xs $ tail xs

入出力結果(Terminal, runghc)

$ runghc Sample.hs temp.txt
the
in
file
$ cat temp.txt
!! @@ ## $$ %%
in the the file
in in the file
in the file file !!
in the file
$

慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。

0 コメント:

コメントを投稿