programming in haskell プログラミング haskell

Download PROGRAMMING IN HASKELL プログラミング Haskell

Post on 02-Jan-2016

55 views

Category:

Documents

3 download

Embed Size (px)

DESCRIPTION

PROGRAMMING IN HASKELL プログラミング Haskell. Chapter 10 - Declaring Types and Classes 型とクラスの定義. 愛知県立大学 情報科学部 計算機言語論 ( 山本晋一郎・大久保弘崇、 2011 年 ) 講義資料 オリジナルは http://www.cs.nott.ac.uk/~gmh/book.html を参照のこと. 型宣言 (Type Declarations). 型宣言を用いて既存の型に別名を付けることができる. type String = [Char]. - PowerPoint PPT Presentation

TRANSCRIPT

  • *PROGRAMMING IN HASKELLHaskellChapter 10 - Declaring Types and Classes (2011) http://www.cs.nott.ac.uk/~gmh/book.html

  • *(Type Declarations)type String = [Char]String [Char]

  • *origin :: Posorigin = (0,0)

    left :: Pos Posleft (x,y) = (x-1,y)type Pos = (Int,Int):

  • * type Pair a = (a,a):mult :: Pair Int Intmult (m,n) = m*n

    copy :: a Pair acopy x = (x,x)

  • *:type Pos = (Int,Int)

    type Trans = Pos Pos(type):type Tree = (Int,[Tree])

  • *(Data Declarations)data Bool = False | TrueBool 2 False True

  • *Note: False True Bool ()(data constructor)

    :

  • *answers :: [Answer]answers = [Yes,No,Unknown]

    flip :: Answer Answerflip Yes = Noflip No = Yesflip Unknown = Unknowndata Answer = Yes | No | Unknown:

  • *data Shape = Circle Float | Rect Float Floatsquare :: Float Shapesquare n = Rect n n

    area :: Shape Floatarea (Circle r) = pi * r^2area (Rect x y) = x * y:

  • *:Shape Circle rr Float Rect x yx y Float

    Circle Rect Shape :Circle :: Float Shape

    Rect :: Float Float ShapeRect 2 Float Shape Rect

  • *data Maybe a = Nothing | Just asafediv :: Int Int Maybe Intsafediv _ 0 = Nothingsafediv m n = Just (m `div` n)

    safehead :: [a] Maybe asafehead [] = Nothingsafehead xs = Just (head xs):() : Int a

  • (case)()( [] cons (:))

    Maybe ( Nothing Just)*case exp of [] (x:xs) f :: [a] f [] = f (x:xs) =

  • *data Nat = Zero | Succ NatNat 2 Zero :: Nat Succ :: Nat Nat

  • *Note:Nat Zero Succ n (n :: Nat)Nat :ZeroSucc ZeroSucc (Succ Zero)

  • *Nat Zero 0 Succ 1 (1+)

    Succ (Succ (Succ Zero)) 3

  • *Nat Int :nat2int :: Nat Intnat2int Zero = 0nat2int (Succ n) = 1 + nat2int n

    int2nat :: Int Natint2nat 0 = Zeroint2nat n = Succ (int2nat (n 1))n+kint2nat (n+1) = Succ (int2nat n)

  • *2 Nat Int Nat : add :add :: Nat Nat Natadd m n = int2nat (nat2int m + nat2int n)add Zero n = nadd (Succ m) n = Succ (add m n)

  • *:add (Succ (Succ Zero)) (Succ Zero)

  • *

  • * ::data Expr = Val Int | Add Expr Expr | Mul Expr ExprAdd (Val 1) (Mul (Val 2) (Val 3))

  • *:size :: Expr Intsize (Val n) = 1size (Add x y) = size x + size ysize (Mul x y) = size x + size y

    eval :: Expr Inteval (Val n) = neval (Add x y) = eval x + eval yeval (Mul x y) = eval x * eval y

  • *Note:3 :Val :: Int ExprAdd :: Expr Expr ExprMul :: Expr Expr Expr fold :eval = fold id (+) (*)

  • fold Val id Add (+) Mul (*) eval *fold' v a m (Val x) = v xfold' v a m (Add x y) = a (fold' v a m x) (fold' v a m y)fold' v a m (Mul x y) = m (fold' v a m x) (fold' v a m y) fold id (+) (*) (Add (Val 1) (Mul (Val 2) (Val 3)))= (+) (id 1) ((*) (id 2) (id 3))= 7fold' id (+) (*)id :: x -> xid x = x

  • * 2

  • *::data Tree = Leaf Int | Node Tree Int TreeNode (Node (Leaf 1) 3 (Leaf 4)) 5 (Node (Leaf 6) 7 (Leaf 9))

  • * :occurs :: Int Tree Booloccurs m (Leaf n) = m==noccurs m (Node l n r) = m==n || occurs m l || occurs m r

  • * flatten (()()):flatten :: Tree [Int]flatten (Leaf n) = [n]flatten (Node l n r) = flatten l ++ [n] ++ flatten r flatten (search tree) flatten [1,3,4,5,6,7,9]

  • * 2 : occurs 1 ()occurs m (Leaf n) = m==noccurs m (Node l n r) | m==n = True | mn = occurs m r

  • class a Eq (==) (/=) (/=) (==) (/=)

    (==) Bool Eq *class Eqawhere (==),(/=) ::a->a->Boolx /= y= not (x == y)instance Eq Bool where False == False= True True == True= True _ == _= False

  • deriving

    : data Bool = False | True deriving (Eq, Ord, Show, Read)Bool Eq *

  • (10): type String = [Char]: (Answer3)data Answer = Yes | No | Unknowndata Shape = Circle Float| Rect Float Float(Maybe)data Maybe a = Nothing | Just acase

    data Nat = Zero | Succ Nat

    *()

    *