カテゴリ
全体プログラミング scheme verilog 未分類 以前の記事
2016年 04月2016年 03月 2016年 02月 2016年 01月 2015年 12月 2015年 11月 2015年 10月 2015年 09月 2015年 08月 2015年 07月 2015年 06月 2015年 03月 お気に入りブログ
PHPで競技プログラミングメモ帳
最新のトラックバック
ライフログ
検索
タグ
Racket
その他のジャンル
ブログパーツ
最新の記事
外部リンク
ファン
記事ランキング
ブログジャンル
画像一覧
|
-- ch3.hs
-- 1. -- ['a', 'b', 'c'] の型は [Char] -- ('a', 'b', 'c') の型は (Char, Char, Char) -- [(False, 'o'), (True, '1')] の型は [(Bool, Char)] -- ([False, True], ['0', '1']) の型は ([Bool], [Char]) -- [tail, init, reverse] の型は [[a] -> [a]] -- 2. -- second :: [a] -> a -- swap :: (a, b) -> (b, a) -- pair :: a -> b -> (a, b) -- double :: Num a => a -> a -- palindrome :: Eq a => [a] -> Bool -- twice :: (a -> a) -> a -> a -- 3. {- -- GHCiで実行 -- 値 : 型 とインタプリタに入力してみる。 *Main> ['a', 'b', 'c'] :: [Char] "abc" -- また、わざと間違えてみる。するとエラーになる。 *Main> ['a', 'b', 'c'] :: (Char, Char, Char) <interactive>:38:1: Couldn't match expected type ‘(Char, Char, Char)’ with actual type ‘[Char]’ In the expression: ['a', 'b', 'c'] :: (Char, Char, Char) In an equation for ‘it’: it = ['a', 'b', 'c'] :: (Char, Char, Char) *Main> ('a', 'b', 'c') :: (Char, Char, Char) ('a','b','c') *Main> [(False, 'o'), (True, '1')] :: [(Bool, Char)] [(False,'o'),(True,'1')] *Main> ([False, True], ['0', '1']) :: ([Bool], [Char]) ([False,True],"01") *Main> [tail, init, reverse] :: [[a] -> [a]] <interactive>:42:1: No instance for (Show ([a0] -> [a0])) (maybe you haven't applied enough arguments to a function?) arising from a use of ‘print’ In a stmt of an interactive GHCi command: print it -- 関数のリストの場合、関数が表示クラスでないためエラーがでるようだ。 -- そこで、またわざと間違った値と型の組み合わせを入力する。 *Main> [head, init, reverse] :: [[a] -> [a]] <interactive>:45:2: Couldn't match type ‘a1’ with ‘[a1]’ ‘a1’ is a rigid type variable bound by an expression type signature: [[a1] -> [a1]] at <interactive>:45:1 Expected type: [a1] -> [a1] Actual type: [[a1]] -> [a1] In the expression: head In the expression: [head, init, reverse] :: [[a] -> [a]] -- 先ほどとは異なるエラーがでる。 -} -- GHCiで実行 second :: [a] -> a second xs = head (tail xs) {- この定義はエラー second' :: [a] -> b second' xs = head (tail xs) -} swap :: (a, b) -> (b, a) swap (x, y) = (y, x) -- この定義はOK swap' :: (a, a) -> (a, a) swap' (x, y) = (y, x) pair :: a -> b -> (a, b) pair x y = (x, y) double :: Num a => a -> a double x = x * 2 {- この定義はエラー double' :: a -> a double' x = x * 2 -} palindrome :: Eq a => [a] -> Bool palindrome xs = reverse xs == xs {- この定義はエラー palindrome' :: [a] -> Bool palindrome' xs = reverse xs == xs -} twice :: (a -> a) -> a -> a twice f x = f (f x) {- この定義はエラー twice' :: (a -> b) -> a -> b twice' f x = f (f x) -} -- 4. {- Haskellの組み込み関数がC/C++のライブラリで実現されているため。 実現可能であるのは、 組み込み関数f1, .., fnについて、 それらの関数のうち任意のfiがほかの組み込み関数の組み合わせC(f1..fi-1,fi+1,..,fn)と 恒等関数とならず、また、組み込み関数の組み合わせを正規化できる場合。 -} ■
[PR]
by tempurature
| 2015-12-30 23:55
| プログラミング
| ||||||||||||||||||||||
ファン申請 |
||
外部サイトRSS追加 |
||