Real World Haskell - 第3章 Part6 Parameterised types, Recursive types
Real World Haskell で Haskell を継続的に勉強中。
Defining Types, Streamlining Functions
Chapter 3. Defining Types, Streamlining Functions
Parameterised types
- 独自に定義した型でも list type と同様にポリモーフィズム可能
- 型定義で型変数を利用できる
data Maybe a = Just a | Nothing
ghci> Just 1.5 Just 1.5 ghci> Nothing Nothing ghci> :type Just "invisible bike" Just "invisible bike" :: Maybe [Char]
Recursive types
- list type と同様の、再帰的な型を作ることができる
data List a = Cons a (List a) | Nil deriving (Show)
ghci> Cons 0 Nil Cons 0 Nil ghci> Cons 1 it Cons 1 (Cons 0 Nil) ghci> Cons 2 it Cons 2 (Cons 1 (Cons 0 Nil)) ghci> Cons 3 it Cons 3 (Cons 2 (Cons 1 (Cons 0 Nil)))
- binary tree も簡単に書けるよ
data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show)
参考
Haskell Language
Haskell - Wikipedia
Haskell(ハスケル)は非正格な動作を持つ純粋関数型プログラミング言語である。名称は論理学者であるハスケル・カリー(Haskell B. Curry)に由来する。