開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Haskell (プログラミング言語)
- Haskell Platform(ghci)(処理系)
関数プログラミング入門(Richard Bird (著)、山下伸夫 (翻訳)、オーム社)の第4章(リスト)、4.1(リストの表記法)、練習問題4.1.1、4.1.2、4.1.3、4.1.4を取り組んでみる。
練習問題4.1.1、4.1.2、4.1.3、4.1.4
コード(Emacs)
-- 4.1.1 a :: [Bool] a = [] b :: [Char] b = [] expr :: ([Bool], [Char]) expr = (a, b) -- 4.1.2 -- instance (Ord a) => Ord [a] where -- (<) [] [] = False -- (<) xs [] = True -- (<) (x:xs) (y:ys) = if x < y -- then xs < ys -- else False -- 4.1.3 last (x:xs) = if null xs then x else Main.last xs last' (x:xs) = if xs == [] then x else last' xs e = [id] -- 4.1.4 head' (x:xs) = x data Liste a = Nil | Snoc (Liste a) a heade' (Snoc Nil x) = x heade' (Snoc xs _) = heade' xs nums = [1, 2, 3] nums' = Snoc (Snoc (Snoc Nil 1) 2) 3 convert :: Liste a -> [a] convert Nil = [] convert (Snoc xs x) = (convert xs) ++ [x] main :: IO () main = do print expr print ((Main.last e) 10) print (head' nums) print (heade' nums') print (convert nums')
入出力結果(Terminal, ghci, runghc)
$ runghc sample1.hs ([],"") 10 1 1 [1,2,3] $
0 コメント:
コメントを投稿