第2章 言語プログラミングの言語

download 第2章 言語プログラミングの言語

If you can't read please download the document

  • date post

    26-Jan-2016
  • Category

    Documents

  • view

    30
  • download

    4

Embed Size (px)

description

第2章 言語プログラミングの言語. Haskell Programming Language. 言語の定義には言語が必要. 言語の記述にプログラミング言語を使用 言語機能の記述はプログラムとして実行可能 抽象的な言語の概念を具体例を通じて確認 関数型言語による言語プログラミング 関数でアルゴリズムを表現する 複雑な概念を簡潔に表現 関数型言語の基本的な概念の理論的な基盤が確立されている. 関数型言語 Haskell. ホームページ http://www.haskell.org/ システム Hugs - PowerPoint PPT Presentation

Transcript of 第2章 言語プログラミングの言語

  • Haskell Programming Language

  • Haskellhttp://www.haskell.org/Hugshttp://haskell.cs.yale.edu/hugs/Bird,R. and Wadler,P.: Introduction to Functional Programming, Prentice-Hall, 1988. (: , , , 1991, ISBN4-7649-0181-1)

  • Prelude> 3*824Prelude> 2+3*826Prelude> 2^3/51.6Prelude> 3 / 50.6Prelude> mod 3 21Prelude> 3 `mod` 21Prelude> 3 `div` 21

  • x = 3*8y = x+21E:\work\2000\00\2-1.hsE:\work\2000\00\2-1.lhsYou can write anything here.> x = 3*8> y = x+21

  • (local declaration): where

    : let in

    x = y * (y + 5) where y = 3 let y = 3 in y * (y + 5)

  • : ()

  • f :: a -> b

  • square :: Int -> Int square x = x * x square 5 square 5 + 2

  • \x -> x * x square = \x -> x * x (\x -> x * x) 5

  • 2

    (section): (*) 3 8 = (3*) 8 = (*8) 3 = 3*8

  • 2 ()

    sumsquares x y = square x + square y where square z = z * z sumsquares 3 43 `sumsquare` 4

  • (precedence):: -, /: ^: +, *x *+ y = x * 10 + y

  • infixl 9 !!infixr 9 . infixr 8 ^ infixl 7 * infix 7 /, `div`, `rem`, `mod`infixl 6 +, - infix 5 \\ infixr 5 ++ infix 4 ==, /=, < , < =, > , > = infix 4 `elem`, `notElem` infixr 3 && infixr 2 ||

  • (higher order function) (functional composition)(f . g) x = f (g x) (partial application)2 t1-> (t2 -> ( ... -> (tn-> t) ) ) (1+)

  • factorial 0 = 1 factorial (n+1) = n * factorial n factorial n = if x==0 then 1 else n*factorial(n-1)

  • :

  • Int Bool Char Float

  • Int : 0,1,...,910 +,-,*,`div`,`rem` negate :: Int -> Int odd :: Int -> Bool even :: Int -> Bool

  • Bool TrueFalse ||, & & not :: Bool -> Bool

  • Char1ord :: Char -> Intchr :: Int -> Char

  • Float

  • : [t] [t] [ ][1,2,3,4,5][1..5] [] :: [t](:) :: t -> [t] -> [t]

  • ++[1,2,3]++[4,5] = [1,2,3,4,5]++ (list comprehension) [x+1 | x < - [1,2,3]] [x+y | x < - [10,20,30], y < - [4,5]] [ [x | x < - [0..9], even x] [x-y | x < - [1,2,3], y < - [ ]]

  • " String ([Char])Hello ++ world! = Hello world!

  • : ( t1, t2, ..., tn ) (1,2) (1,True,[2,3,4]) fst :: (t1,t2) -> t1snd :: (t1,t2) -> t2

  • : s -> t :

  • (currying)

    f :: (Int,Int) -> Int f (x,y) = x^2 + y^2 f' :: Int -> Int -> Int f' x y = x^2 + y^2 curry :: ((a,b) -> c) -> (a -> b -> c) curry f x y = f (x,y) Uncurry?

  • (type synonym): type IntPair = (Int,Int) T = Int | Bool | Char | Float | t | [T] | (T1,T2,,Tn) | T1 -> T2

  • (constructor)

  • data Day = Sun | Mon | Tue | Wed | Thu | Fri | Sat workday :: Day -> Boolworkday Sun = False workday Mon = True workday Tue = True workday Wed = True workday Thu = True workday Fri = True workday Sat = False

  • data Figure = Circle Float Rectangle Float Float Circle, Rectangle 5.2 : Circle 5.2 3.2 2.5

  • Peano data Nat = Zero | Succ Nat

    : a data List a = Nil | Cons a (List a)

  • area :: Figure -> Float area (Circle r) = 3.14 * r ^ 2 area (Rectangle x y) = x * y

  • head :: [a] -> a head (x:xs) = x tail :: [a] -> [a] tail (x:xs) = xs sum :: [Int] -> Intsum [] = 0sum (x:xs) = x + sum xs

  • fst :: (a,b) -> a fst (x,_) = x snd :: (a,b) -> b snd (_,y) = y fst3 :: (a,b,c) -> a fst3 (x,_,_) = x snd3 :: (a,b,c) -> b snd3 (_,y,_) = y thd3 :: (a,b,c) -> c thd3 (_,_,z) = z

  • (map function)

    >map (1+) [1,2,3]

    map :: (a -> b) -> [a] -> [b] map f xs = [ f x | x < - xs ]

  • (filter function)

    filter even [0..9] ilter :: (a -> Bool) -> [a] -> [a] filter p xs = [ x | x < - xs, p x ]

  • (fold function) foldl () e [x1,x2, ..., xn] = (((e x1) x2) ...) xn foldr () e [x1,x2, ..., xn] = x1 (x2 ( ... (xn e)))

    sum :: [Int] -> Int sum = foldl (+) 0 product :: [Int] -> Int product = foldl (*) 1 concat :: [[a]] -> [a] concat = foldr (++) [ ]

  • (abstract type): Assoc a b None :: Assoc a bLoopup :: Eq a => Assoc a b -> a -> bUpdate :: Eq a => Assoc a b -> a -> b -> Assoc a b

  • Assoc a b type Assoc a b = a -> b

    none :: Assoc a b none x = undefined

    lookup :: Assoc a b -> a -> b lookup h x = h x

    update :: Eq a => Assoc a b -> a -> b -> Assoc a b update h x v y | x==y = v | otherwise = lookup h y

  • 12.1 2.2 ()() 2.3

  • 22.4 : : 2.5 2.6 2.7 2.8

    Sin, cos: functions