開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Haskell (純粋関数型プログラミング言語)
- GHC (The Glasgow Haskell Compiler) (処理系)
- The Haskell Platform (インストール方法、モジュール等)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の17章(高度なポインタ)、17-12(プログラミング実習)、実習17-1.をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
17-12(プログラミング実習)、実習17-1.
コード(BBEdit)
Sample.hs
{-# OPTIONS -Wall -Werror #-} import System.Environment main :: IO () main = do (a:[]) <- getArgs ls <- fmap lines $ readFile a let ps = concat $ zipWith (\n ws -> map (\w -> (n, w)) ws) ([1..length ls] :: [Int]) (map words ls) putStr $ show $ foldr treeInsert EmptyTree ps data Tree ns a = EmptyTree | Node ns a (Tree ns a) (Tree ns a) instance (Show ns, Show a) => Show (Tree ns a) where show EmptyTree = "" show (Node ns a left right) = show left ++ show a ++ ':':show ns ++ '\n':show right singleton :: (Int, a) -> Tree [Int] a singleton (n, a) = Node [n] a EmptyTree EmptyTree treeInsert :: (Ord a) => (Int, a) -> Tree [Int] a -> Tree [Int] a treeInsert (n, x) EmptyTree = singleton (n, x) treeInsert (n, x) (Node ms a left right) | x == a = Node (ms ++ [n]) x left right | x < a = Node ms a (treeInsert (n, x) left) right | x > a = Node ms a left (treeInsert (n, x) right) | otherwise = EmptyTree
入出力結果(Terminal, runghc)
$ runghc Sample.hs "!=":[135] "\"":[139,137] "\",":[128,33] "\"Error:Out":[19] "\"Error:Unable":[92] "\"Error:Wrong":[136] "\"Usage":[138] "\"r\");":[90] "#include":[4,3,2,1] "%s\\n\",":[92] "'\\0';":[116] "'\\n'){":[98] "'file'\\n\");":[139] "(!isalpha(ch)){":[111] "((*node)":[59,57] "(*node)":[58] "(*node)->left":[64] "(*node)->right":[65] "(*node)->word":[62] "(0);":[145] "(1){":[95] "(8);":[20] "(ch":[105,101,98] "(in_file":[91] "(index":[109] "(new_string":[44] "(new_string);":[48] "(result":[76,71] "(strlen(string)":[42] "(top":[124] "**node,":[51] "*argv[])":[133] "*in_file;":[88] "*left;":[10] "*name)":[83] "*new_string;":[40] "*node)":[29] "*node,":[56,23] "*right;":[11] "*root":[14] "*save_string();":[54] "*save_string(char":[38] "*string)":[38] "*top)":[122] "*word)":[51] "*word;":[7] "+":[42,26] "++index){":[109] "0){":[76,71] "0;":[32] "1));":[42] "1;":[109,26,15] "2){":[135] ":":[143] "<":[109,76,32] "<ctype.h>":[2] "<stdio.h>":[1] "<stdlib.h>":[4] "<string.h>":[3] "=":[116,114,110,109,108,97,90,69,65,64,62,58,42,32,26,25,15,14] "==":[124,105,101,98,91,71,59,57,44] "EOF)){":[101] "EOF){":[105] "FILE":[88] "NULL){":[124,91,59,57,44] "NULL;":[65,64,14] "add_list((*node),":[72,63] "add_list(struct":[56,23] "argc,":[133] "break;":[112,106,102] "ch":[110,97] "ch;":[114,108,87] "char":[133,85,54,51,40,38,7] "command":[137] "current_lineno":[15] "current_lineno);":[72,63] "current_lineno++;":[99] "else":[78] "enter(&(*node)->left,":[79] "enter(&(*node)->right,":[77] "enter(&root,":[117] "enter(struct":[51] "exit":[20] "exit(8);":[140,93] "fclose(in_file);":[119] "fgetc(in_file);":[110,97] "fopen(name,":[90] "for":[109] "for(j":[32] "fprintf(stderr,":[139,138,137,136,92,19] "i;":[8] "if":[124,111,105,98,91,76,71,59,57,44] "if(argc":[135] "if(isalpha(ch)":[101] "in_file":[90] "index":[109] "index;":[86] "int":[133,87,86,56,53,31,23,15,9,8] "is:\\n\");":[138] "j":[32] "j++){":[32] "j;":[31] "line\\n\");":[137] "linenos[100];":[9] "main(int":[133] "malloc((unsigned)":[42] "malloc(sizeof(struct":[58] "memory\\n\");":[19] "memory_error();":[60,55,45] "memory_error(void)":[17] "n)":[23] "n);":[56] "n;":[25] "name);":[92] "new_string":[42] "node":[122,56,51,29,23,14,11,10] "node));":[58] "node->i":[26,26] "node->i;":[32] "node->linenos[j]);":[33] "node->linenos[node->i]":[25] "node{":[6] "numbr":[136] "of":[136,19] "on":[137] "open":[92] "parameters\\n\");":[136] "print_lineno(struct":[29] "print_lineno(top);":[129] "print_tree(root);":[144] "print_tree(struct":[122] "print_tree(top->left);":[127] "print_tree(top->right);":[130] "printf(\"%-10s:":[128] "printf(\"%d":[33] "printf(\"\\n\");":[35] "printf(\"\21336\35486":[143] "result":[69] "result;":[53] "return":[145,48] "return;":[125,73,66] "save_string(word);":[62] "scan(argv[1]);":[142] "scan(char":[83] "sizeof(word);":[109] "static":[15,14] "strcmp((*node)->word,":[69] "strcpy(new_string,":[47] "string);":[47] "struct":[14,11,10,6] "the":[137] "to":[92] "top->word);":[128] "void":[122,83,56,55,51,29,23,17] "while":[95] "while(1){":[96] "word);":[117,79,77,69] "word[0]":[108] "word[100];":[85] "word[index]":[116,114] "words":[139] "{":[134,123,84,78,52,39,30,24,18] "||":[101] "}":[146,141,131,126,120,118,115,113,107,104,103,100,94,81,80,78,74,67,61,49,46,36,34,27,21] "};":[12] "\34892\\n\");":[143] $
慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。
0 コメント:
コメントを投稿