開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- Haskell (純粋関数型プログラミング言語)
- GHC (The Glasgow Haskell Compiler) (処理系)
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の13章(新しいクラスの作成と既存クラスの変更), 13.1(練習問題)、組み込みクラスの拡張をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
Haskellだと組み込みクラスとかオブジェクトとかRubyと考え方が違うので、とりあえずモジュールお作ってみることに。
モジュールの作成(import, export, module () where)
コード(BBEdit)
sample.hs
{-# OPTIONS -Wall -Werror #-} import SampleModule1 main :: IO () main = do putStrLn $ show $ shuffle $ map show ([1, 2..10] :: [Int]) putStrLn $ show $ factorial 10 putStrLn $ show $ romanNumeral 1294
コード(BBEdit)
SampleModule1.hs
{-# OPTIONS -Wall -Werror #-} module SampleModule1 ( factorial, romanNumeral, shuffle, ) where import qualified System.Random as Random factorial :: Int -> Maybe Int factorial a | a < 0 = Nothing | otherwise = Just $ foldr (\x acc -> x * acc) 1 [1..a] romanNumeral :: Int -> Maybe String romanNumeral a | a >= 0 = Just $ iter (mod a 1000) numRomanPairs 'M' $ replicate (div a 1000) 'M' | otherwise = Nothing iter :: Int -> [(Int, (Char, Char))] -> Char -> String -> String iter _ [] _ s = s iter n ((b,(c,d)):xs) e s = let f = div n b g = mod n b h = replicate (div f 5) c i = replicate (mod f 5) d in case f of 9 -> iter g xs d (s ++ (d:[e])) 4 -> iter g xs d (s ++ (d:[c])) _ -> iter g xs d (s ++ h ++ i) numRomanPairs :: [(Int, (Char, Char))] numRomanPairs = [(100, ('D', 'C')), (10, ('L', 'X')), (1, ('V', 'I'))] shuffle :: [String] -> [String] shuffle [] = [] shuffle xs = let a = length xs r = fst $ Random.randomR (0, a - 1) (Random.mkStdGen 0) in (xs !! r):(shuffle $ (take r xs) ++ (drop (r + 1) xs))
入出力結果(Terminal, runghc)
$ runghc sample.hs ["6","10","7","1","9","2","4","8","5","3"] Just 3628800 Just "MCCXCIV" $
0 コメント:
コメントを投稿