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)の4章(関数プログラミング)の4.6(ループをどのように考えるか)の練習問題5.を解いてみる。
5.
コード(BBEdit)
Sample.hs
-- file: Sample.hs myTakeWhile :: (a -> Bool) -> [a] -> [a] myTakeWhile p (x:xs) | p x = x:(myTakeWhile p xs) | otherwise = [] myTakeWhile p [] = [] myTakeWhile' :: (a -> Bool) -> [a] -> [a] myTakeWhile' p xs = foldr step [] xs where step x ys | p x = x:ys | otherwise = [] a = [1,2,3,4,5] b = [1,3,2,4,5] c = [2,1,3,4,5] d = [] e = [1] f = [2]
入出力結果(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> takeWhile odd a [1] *Main> myTakeWhile odd a [1] *Main> myTakeWhile' odd a [1] *Main> takeWhile odd b [1,3] *Main> myTakeWhile odd b [1,3] *Main> myTakeWhile' odd b [1,3] *Main> takeWhile odd c [] *Main> myTakeWhile odd c [] *Main> myTakeWhile' odd c [] *Main> takeWhile odd d [] *Main> myTakeWhile odd d [] *Main> myTakeWhile' odd d [] *Main> takeWhile odd e [1] *Main> myTakeWhile odd e [1] *Main> myTakeWhile' odd e [1] *Main> takeWhile odd f [] *Main> myTakeWhile odd f [] *Main> myTakeWhile' odd f [] *Main> :quit Leaving GHCi. $
0 コメント:
コメントを投稿