開発環境
- 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 (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の15章(デバッグと最適化)、15.8(プログラミング実習)、実習 15-1をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
実習15-1.
コード(BBEdit)
Sample.hs
{-# OPTIONS -Wall -Werror #-}
main :: IO ()
main = mapM_ putStrLn $ map (\(x, y) -> (show $ ones !! x !! y) ++ ", " ++
(show $ minusOnes !! x !! y))
[(x, y) | x <- [0..4], y <- [0..4]]
ones :: [[Int]]
ones = map (\_ -> take 32 [1, 1..]) ([1..60] :: [Int])
minusOnes :: [[Int]]
minusOnes = map (\_ -> take 32 [-1, -1..]) ([1..60] :: [Int])
入出力結果(Terminal, runghc)
$ ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l Sample
[1 of 1] Compiling Main ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break ones
Breakpoint 0 activated at Sample.hs:10:8-54
*Main> main
Stopped at Sample.hs:10:8-54
_result :: [[Int]] = _
[Sample.hs:10:8-54] *Main> :step
Stopped at Sample.hs:10:19-34
_result :: [Int] = _
[Sample.hs:10:19-34] *Main> :step
Stopped at Sample.hs:10:27-34
_result :: [Int] = _
[Sample.hs:10:27-34] *Main> :show breaks
[0] Main Sample.hs:10:8-54
[Sample.hs:10:27-34] *Main> :delete [0]
[Sample.hs:10:27-34] *Main> :break minusOnes
Breakpoint 1 activated at Sample.hs:13:13-61
[Sample.hs:10:27-34] *Main> :continue
1, Stopped at Sample.hs:13:13-61
_result :: [[Int]] = _
[Sample.hs:13:13-61] *Main> :step
Stopped at Sample.hs:13:24-41
_result :: [Int] = _
[Sample.hs:13:24-41] *Main> :step
Stopped at Sample.hs:13:32-41
_result :: [Int] = _
[Sample.hs:13:32-41] *Main> :step
Stopped at Sample.hs:13:33-34
_result :: Int = _
[Sample.hs:13:33-34] *Main> :step
Stopped at Sample.hs:13:37-38
_result :: Int = _
[Sample.hs:13:37-38] *Main> :step
-1
Stopped at Sample.hs:(4,41)-(5,68)
_result :: [Char] = _
x :: Int = 0
y :: Int = 1
[Sample.hs:(4,41)-(5,68)] *Main> :show breaks
[0] Main Sample.hs:10:8-54
[1] Main Sample.hs:13:13-61
[Sample.hs:(4,41)-(5,68)] *Main> :delete [0]
[Sample.hs:(4,41)-(5,68)] *Main> :delete [1]
[Sample.hs:(4,41)-(5,68)] *Main> :show breaks
[0] Main Sample.hs:10:8-54
[1] Main Sample.hs:13:13-61
[Sample.hs:(4,41)-(5,68)] *Main> :delete *
[Sample.hs:(4,41)-(5,68)] *Main> :show breaks
No active breakpoints.
[Sample.hs:(4,41)-(5,68)] *Main> :break 4
Breakpoint 2 activated at Sample.hs:4:8-21
[Sample.hs:(4,41)-(5,68)] *Main> :show breaks
[2] Main Sample.hs:4:8-21
[Sample.hs:(4,41)-(5,68)] *Main> :continue
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
*Main> :break Sample.hs 4
Top level:
attempting to use module `Sample' (./Sample.hs) which is not loaded
*Main> :l Sample
[1 of 1] Compiling Main ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break Sample.hs 4
Top level: Not in scope: `Sample.hs'
*Main> :break 4
Breakpoint 3 activated at Sample.hs:4:8-21
*Main> ain
<interactive>:29:1:
Not in scope: `ain'
Perhaps you meant one of these:
`main' (line 4), `asin' (imported from Prelude),
`sin' (imported from Prelude)
*Main> :main
Stopped at Sample.hs:4:8-21
_result :: [String] -> IO () = _
[Sample.hs:4:8-21] *Main> :show breaks
[3] Main Sample.hs:4:8-21
[Sample.hs:4:8-21] *Main> :continue
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
1, -1
*Main> :q
Leaving GHCi.
$
慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。
0 コメント:
コメントを投稿