Real World Haskell
実戦で学ぶ関数型言語プログラミング
Bryan O'Sullivan, John Goerzen, Don Stewart(著)
山下 伸夫, 伊東 勝利
株式会社タイムインターメディア(翻訳)
開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Haskell (純粋関数型)
Real World Haskell』(Bryan O'Sullivan、John Goerzen、Don Stewart(著)、山下 伸夫、伊東 勝利、株式会社タイムインターメディア(翻訳)、オライリー・ジャパン、2009年、ISBN978-4-87311-423-3)の3章(型を定義し、関数を単純化する)の3.7(再帰型)の練習問題1.を解いてみる。
1.
コード(BBEdit)
sample.hs
-- file: sample.hs data List a = Cons a (List a) | Nil deriving (Show) fromList (x:xs) = Cons x (fromList xs) fromList [] = Nil toList (Cons x xs) = x : (toList xs) toList Nil = []
入出力結果(Terminal)
$ ghci GHCi, version 7.4.2: 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> :load sample.hs [1 of 1] Compiling Main ( sample.hs, interpreted ) Ok, modules loaded: Main. *Main> :type toList toList :: List a -> [a] *Main> fromList("Haskell") Cons 'H' (Cons 'a' (Cons 's' (Cons 'k' (Cons 'e' (Cons 'l' (Cons 'l' Nil)))))) *Main> toList(it) "Haskell" *Main> fromList([Just True, Nothing, Just False] <interactive>:6:42: parse error (possibly incorrect indentation) *Main> fromList([Just True, Nothing, Just False]) Cons (Just True) (Cons Nothing (Cons (Just False) Nil)) *Main> toList(it) [Just True,Nothing,Just False] *Main> :quit Leaving GHCi. $
0 コメント:
コメントを投稿