org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

Similar documents
3 3.1 algebraic datatype data k = 1 1,1... 1,n1 2 2,1... 2,n2... m m,1... m,nm 1 m m m,1,..., m,nm m 1, 2,..., k 1 data Foo x y = Alice x [y] B

2

haskell.gby

2

Copyright c 2006 Zhenjiang Hu, All Right Reserved.

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

fp.gby

Functional Programming

Parametric Polymorphism


monad.gby

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

bdd.gby

6-1

r07.dvi

ohp07.dvi

presen.gby

Functional Programming

# let rec sigma (f, n) = # if n = 0 then 0 else f n + sigma (f, n-1);; val sigma : (int -> int) * int -> int = <fun> sigma f n ( : * -> * ) sqsum cbsu

1. A0 A B A0 A : A1,...,A5 B : B1,...,B

Microsoft PowerPoint - ProD0107.ppt

5 IO Haskell return (>>=) Haskell Haskell Haskell 5.1 Util Util (Util: Tiny Imperative Language) 1 UtilErr, UtilST, UtilCont, Haskell C

Jacques Garrigue

test.gby

連載 構文解析器結合子 山下伸夫 (( 株 ) タイムインターメディア ) 文字列の分解 1 1 Haskell lines Prelude lines :: String -> [String] lines "" = [] lines s = let (l,s'

第8回 関数

kyoto.gby

r03.dvi

(CC Attribution) Lisp 2.1 (Gauche )


ohp03.dvi

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

ex01.dvi

: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

untitled

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

K227 Java 2

ohp02.dvi

Programming D 1/15

LIFO(last in first out, ) 1 FIFO(first in first out, ) 2 2 PUSH POP : 1

jssst-ocaml.mgp

I 2 tutimura/ I 2 p.1/??

PowerPoint Presentation

r02.dvi

1

untitled

「計算と論理」 Software Foundations その4

ex01.dvi

koba/class/soft-kiso/ 1 λ if λx.λy.y false 0 λ typed λ-calculus λ λ 1.1 λ λ, syntax τ (types) ::= b τ 1 τ 2 τ 1

ML λ λ 1 λ 1.1 λ λ λ e (λ ) ::= x ( ) λx.e (λ ) e 1 e 2 ( ) ML λx.e Objective Caml fun x -> e x e let 1

1.

ML Edinburgh LCF ML Curry-Howard ML ( ) ( ) ( ) ( ) 1

Int Int 29 print Int fmt tostring 2 2 [19] ML ML [19] ML Emacs Standard ML M M ::= x c λx.m M M let x = M in M end (M) x c λx.

Microsoft PowerPoint - IntroAlgDs-06-8.ppt

1. A0 A B A0 A : A1,...,A5 B : B1,...,B

4 (induction) (mathematical induction) P P(0) P(x) P(x+1) n P(n) 4.1 (inductive definition) A A (basis ) ( ) A (induction step ) A A (closure ) A clos

第12回 モナドパーサ

2 1 Web Java Android Java 1.2 6) Java Java 7) 6) Java Java (Swing, JavaFX) (JDBC) 7) OS 1.3 Java Java

第9回 型とクラス

untitled

cpp1.dvi

パズルをSugar制約ソルバーで解く

つくって学ぶプログラミング言語 RubyによるScheme処理系の実装

新・明解Java入門

Java updated

ScalaFukuoka 2017 Backlog.key

Emacs ML let start ::= exp (1) exp ::= (2) fn id exp (3) ::= (4) (5) ::= id (6) const (7) (exp) (8) let val id = exp in

VDM-SL VDM VDM-SL Toolbox VDM++ Toolbox 1 VDM-SL VDM++ Web bool

2018 年度前期プロジェクト活動 Ritsumeikan Compiler Construction 班活動報告書 佐々木章徳青木雅典西見元希松本幸大

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

r1.dvi

Condition DAQ condition condition 2 3 XML key value

解きながら学ぶJava入門編

プログラミングD - Java

Functional Programming

CX-Checker CX-Checker (1)XPath (2)DOM (3) 3 XPath CX-Checker. MISRA-C 62%(79/127) SQMlint 76%(13/17) XPath CX-Checker 3. CX-Checker 4., MISRA-C CX- Ch

「計算と論理」 Software Foundations その3

r08.dvi

untitled

0 1 q

ohp08.dvi

FBhyou1_140324

2 2.1 NPCMJ ( (Santorini, 2010) (NPCMJ, 2016) (1) (, 2016) (1) (2) (1) ( (IP-MAT (CONJ ) (PP (NP (D ) (N )) (P )) (NP-SBJ *

ohp11.dvi

listings-ext

r11.dvi

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;

SML#³«È¯ºÇÁ°Àþ

slide9.dvi

2 objective m a ( ) Haskell Rank2Types 2 newtype Object f g = Object { runobject :: forall a. f a -> g (a, Object f g) } 1 a g a Functor g a ::

1 (2 * 3) 1 2 * 3 Preorder In order Post order 1 * 1 * Breadth-first Depth-first * * 3 Preorder: 1 * 2 3 In order: 1 2 * 3 Post orde

Pascal Pascal Free Pascal CPad for Pascal Microsoft Windows OS Pascal

program.dvi

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

4 ソフトウェア工学 Software Engineering 抽象データ型 ABSTRACT DATA TYPE データ抽象 (data abstraction) 目的 : データ構造を ( 実装に依存せずに ) 抽象的に定義 方法 : データにアクセス (read, write) する関数の仕様

shift/reset [13] 2 shift / reset shift reset k call/cc reset shift k shift (...) k 1 + shift(fun k -> 2 * (k 3)) k 2 * (1 + 3) 8 reset shift reset (..

text_08.dvi

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

Transcription:

3 Haskell Haskell Haskell 1. 2. 3. 4. 5. 1. 2. 3. 4. 5. 6. C Java 3.1 Haskell Haskell GHC (Glasgow Haskell Compiler 1 ) GHC Haskell GHC http://www.haskell. 1 Guarded Horn Clauses III - 1

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t expr :cd directory :c :! command command :? :quit :q GHCi Prelude> 1+2 3 3 Haskell 3.3 Haskell Haskell http://www.haskell.org/ Haskell Haskell C = III - 2

{ } 1 = 1 ; 2 = 2 ;... ; n = n { } ; C _ Haskell 1 module where module where { 1 = 1 ; 2 = 2 ;... ; n = n } import 1 1 module where Main Haskell 1 = 1 2 = 2... n = n Haskell λ Y=. λx.x λxy.y Haskell III - 3

C c0 = \ f x -> x c1 = \ f x -> f x c2 = \ f x -> f (f x) true = \ t f -> t false = \ t f -> f = c0 f x = x c1 f x = f x c2 f x = f (f x) true t f = t false t f = f Haskell 2 3.4 Haskell Bool Int, Integer Int, Integer Float, Double Char Bool Integer, Float, Double, Char C Lisp +, -, * 1+2*3 if then else if 1 then 2 else 3 1 2 3 1 True 2 False 3 if then else Haskell factorial Haskell fact n = if n==0 then 1 else n*fact(n-1) fact(n-1) fact n-1 n*(fact(n-1)) 3.4.1 fact 100 III - 4

Scheme Lisp Haskell (cons) [] : : 1:2:[] 1:(2:[]), [ ] [1,2,3,4] Integer Double Haskell String type String = [Char] type = type alias String [Char] box-pointer xs0 = [] xs1 = 1:xs0 xs2 = 2:xs1 xs3 = 3:xs2 xs3 xs2 xs1 3 2 1 1 : 2 1, 2, 3 [[1,2],[3,4]] III - 5

1 2 3 4 3.4.2 box-pointer 1. [2,3,5,7,11] 2. [0] 3. [[1],[2,3,4],[]] C struct _list { int car; struct _list* cdr; }; typedef struct _list* list; _list car cdr 2 cdr list typedef struct _list* C C NULL malloc C free Haskell free tuple, ( ) (1, a ) (2, b, [3]) 2 -> Integer -> Char Integer Char -> Bool -> Bool -> Bool 2 Haskell type class III - 6

Integer Char a b [a] -> [b] -> [(a, b)] [Char] -> [Integer] -> [(Char, Integer)] [String] -> [Integer -> Integer] -> [(String, Integer -> Integer)] Haskell :: :: fact fact :: Integer -> Integer fact n = if n==0 then 1 else n*fact(n-1) Haskell 3.5 Haskell 3 -- Prelude length mylength :: [a] -> Integer mylength [] = 0 mylength (x:xs) = 1 + mylength xs mylength [] 1 1 2 x xs mylength length :: [a] -> Int _ mylength x mylength (_:xs) = 1 + mylength xs case of case of case 0 of { 1 -> 1 ; 2 -> 2 ; 3 III - 7

}... n -> n 0 1 1 m m if 1 then 2 else 3 case of case 1 of { True -> 2 ; False -> 3 } let λ \ (x:xs) -> x head 3.5.1 mysum 3.5.2 [Bool] 2 frombin :: [Bool] -> Integer frombin [True, True] 3, frombin [True, False, True, False] 10 : 3.5.3 xs f f sumf :: [Double] -> (Double -> Double) -> Double 3.6 2 reverse: -- append Prelude (++) append :: [a] -> [a] -> [a] append [] ys = ys append (x:xs) ys = x : (append xs ys) -- reverse Prelude reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = append (reverse xs) [x] III - 8

-- shunt rev shunt :: [a] -> [a] -> [a] shunt ys [] = ys shunt ys (x:xs) = shunt (x:ys) xs rev :: [a] -> [a] rev xs = shunt [] xs rev rev reverse xs rev xs = reverse xs shunt ys xs = append (reverse xs) ys : xs = [] : xs = z:zs : 3.6.1 xs append xs (append ys zs) = append (append xs ys) zs xs III - 9

3.7 Prelude map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs zipwith :: (a -> b -> c) -> [a] -> [b] -> [c] zipwith f (x:xs) (y:ys) = f x y : zipwith xs ys zipwith f = [] filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) = if p x then x : filter p xs else filter p xs iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) foldr :: (a -> b -> b) -> b -> [a] -> b foldr f x [] = x foldr f x (y:ys) = f y (foldr f x ys) foldl :: (a -> b -> a) -> a -> [b] -> a foldl f x [] = x foldl f x (y:ys) = foldl (f x y) ys 3.8 Haskell Prelude zip zip :: [a] -> [b] -> [(a,b)] zip (a:as) (b:bs) = (a,b) : zip as bs zip = [] zip [1, 2] [3, 4] [1, 2] zip [3, 4] infixl, infixr, infix III - 10

Prelude Haskell infixr 9. infixl 9!! infixr 8 ˆ, ˆˆ, ** infixl 7 *, /, quot, rem, div, mod, :%, % infixl 6 +, - infixr 5 : infixr 5 ++ infix 4 ==, /=, <, <=, >=, >, elem, notelem infixr 3 && infixr 2 infixl 1 >>, >>= infixr 1 =<< infixr 0 $, $!, seq infixl infixr infix 1 < x < 2 2 * + ( ) 1+2 (+) 1 2 let let in pow4 x = let y = x*x in y*y -- head Prelude head ys = let (x:xs) = ys in x 4 pow4 y*y y -- repeat Prelude repeat :: a -> [a] repeat x = let xs = x:xs in xs x Prelude> repeat 1 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,... 4 Haskell Haskell 3.8.1 evalpoly :: [Double] -> Double -> Double [1, 2, 3, 4] 1 + 2x + 3x 2 + 4x 3 evalpoly [1, 2, 3, 4] 10 4321 4 Ctrl-c III - 11

3.8.2 powerset powerset [1, 2, 3] [[], [1], [2], [3], [1, 2], [2, 3], [1, 3], [1, 2, 3]] 3.9 algebraic datatype data 1 2... k = 1 1,1... 1,n1 2 2,1... 2,n2... m m,1... m,nm C enum data Direction = Up Down Left Right Up, Down, Left, Right 4 Direction data Tree a = Branch (Tree a) a (Tree a) Empty Branch Empty 2 Empty Branch 3 1 3 2 Branch Branch :: a Integer String Tree Integer Integer Tree (type constructor) Tree Empty :: Tree a Branch Empty 1 Empty :: Tree Integer Branch (Branch Empty "a" Empty) "b" (Branch Empty "c" Empty) :: Tree String Branch (Branch Empty 1 Empty) 2 (Branch Empty 3 (Branch Empty 4 Empty)) :: Tree Integer III - 12

1 2 3 4 3.9.1 Tree size :: Tree a -> Integer -- depth :: Tree a -> Integer -- preorder :: Tree a -> [a] -- postorder :: Tree a -> [a] -- reflect :: Tree a -> Tree a -- 3.10 Haskell Haskell twice twice x = x*x twice (2+2) twice (2 + 2) = (2 + 2) (2 + 2) = 4 4 = 16 twice (2+2) twice ( * ) 2+2 2+2 2+2 twice + 2 2 * + 2 2 * 4 16 lazy evaluation III - 13

from :: Integer -> [Integer] from n = n : from (n+1) -- take Prelude take :: Integer -> [a] -> [a] take 0 _ = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs from 1 [1, 2, 3,... ] take 3 (from 1) take 3 (from 1) take 3 (1:from (1+1)) 1:(take 2 (from (1+1))) 1:(take 2 ((1+1):from (1+1+1))) 1:(1+1):(take 1 (from (1+1+1))) 1:(1+1):(1+1+1):(take 0 (from (1+1+1+1))) 1:(1+1):(1+1+1):[] (=[1, 2, 3]) from.. Prelude> [1..] [1,2,3,4,5,6,7,8,9,10,11,... Prelude> [2,4..] [2,4,6,8,10,12,14,16,18,20,22,... Prelude> [1..10] [1,2,3,4,5,6,7,8,9,10] Prelude> [1,4..20] [1,4,7,10,13,16,19] [6] 3.10.1 take n mydrop :: -> [a] Integer -> [a] 3.10.2 fib 3.10.3 2 merge 3.10.4 2 i 3 j 5 k i, j, k 0 hamming 3.10.5 Haskell 3.11 List Comprehension Haskell List Comprehension syntax sugar III - 14

Prelude> [(x, y) x <- [1, 2, 3, 4], y <- [5, 6, 7]] [(1,5),(1,6),(1,7),(2,5),(2,6),(2,7),(3,5),(3,6),(3,7),(4,5),(4,6),(4,7)] Prelude> [x*x x <- [1..10], odd x] [1,9,25,49,81] [1..10] [1,2,3,4,5,6,7,8,9,10] [,..., ] Bool : <- 3.11.1 n 0 x y n x, y foo :: Integer -> [(Integer, Integer)] 3.11.2 n 0 < x < y < z n x 2 + y 2 = z 2 x, y, z chokkaku :: Integer -> [(Integer, Integer, Integer)] -- 1 unit :: a -> [a] unit a = a : [] bind :: [a] -> (a -> [b]) -> [b] bind [] _ = [] bind (x:xs) f = append (f x) (bind xs f) [t] unit t [t x <- u, P] bind u (\ x -> [t P]) [t b, P] if b then [t P] else [] b Bool P qsort [] = [] qsort (x:xs) = qsort [ y y <- xs, y < x] ++ x : qsort [ y y <- xs, y >= x] 3.11.3 unit, bind 1. [(x, y) x <- [1, 2, 3, 4], y <- [5, 6, 7]] 2. [x*x x <- [1..10], odd x] 3.11.4 primes [2, 3, 5, 7, 11,... ] III - 15

: (Eratosthenes) 1. 2 2. 3. 2 C 1000 100 3.12 8 8 8 [4, 6, 1, 5, 2, 8, 3, 7] III - 16

safe p n length p p length p + 1 n safe p n = all not [ check (i, j) (1+length p, n) (i, j) <- zip [1..] p ] check (i,j) (m,n) = j==n (i+j==m+n) (i-j==m-n) all all all p [] all p (x:xs) :: (a -> Bool) -> [a] -> Bool = True = if p x then all p xs else False [1..] [1, 2, 3,... ] > safe [1, 3] 5 True > safe [1, 3] 2 False m ( ) queens III - 17

queens 0 = [[]] queens m = [ append p [n] p<-queens (m-1), n<-[1..8], safe p n ] > queens 1 [[1], [2], [3], [4], [5], [6], [7], [8]] > queens 2 [[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[2,4],[2,5],[2,6],[2,7],[2,8], [3,1],[3,5],[3,6],[3,7],[3,8],[4,1],[4,2],[4,6],[4,7],[4,8],[5,1], [5,2],[5,3],[5,7],[5,8],[6,1],[6,2],[6,3],[6,4],[6,8],[7,1],[7,2], [7,3],[7,4],[7,5],[8,1],[8,2],[8,3],[8,4],[8,5],[8,6]] head (queens 8) > head (queens 8) [1,5,8,6,3,7,2,4] [1, 5, 8, 6, 3, 7, 2, 4] queens 7 queens 7 head (queens 8) Prolog (backtrack) 1 1 queens 8 > queens 8 [[1,5,8,6,3,7,2,4],[1,6,8,3,7,4,2,5], ( ),[8,3,1,6,2,5,7,4],[8,4,1,3,6,2,7,5]] 92 [1] Haskell A Purely Functional Language featuring static typing, higher-order functions, polymorphism, type classes and monadic effects http://www.haskell.org/ Haskell [2] Programming in Haskell http://www.sampou.org/cgi-bin/haskell.cgi Haskell [3] Simon Peyton Jones, John Hughes Haskell 98: A Non-strict, Purely Functional Language 1999 2, http://www.haskell.org/onlinereport/ Haskell Haskell [4] Mark P Jones, Alastair Reid The Hugs 98 User Manual http://cvs.haskell.org/hugs/pages/hugsman/ Haskell Hugs III - 18

[5] Simon Peyton Jones, David Lester Implementing Functional Languages Prentice Hall, 1992 http://research.microsoft.com/users/simonpj/papers/pj-lester-book/ Haskell [6] John Hughes Why Functional Programming Matters 1989, http://www.md.chalmers.se/ rjmh/papers/whyfp.html http://www.sampou.org/haskell/article/whyfp.html [7] Philip Wadler List Comprehensions Simon Peyton Jones The Implementation of Functional Programming Languages Prentice Hall, 1987 http://research.microsoft.com/users/simonpj/papers/slpj-book-1987/ 7 [8] Haskell http://www.ipsj.or.jp/07editj/promenade/ 2005 4 2006 3 [9] Simon Peyton Jones Wearing the hair shirt A retrospective on Haskell 2003, http://research.microsoft.com/ simonpj/papers/haskell%2dretrospective/ Haskell Haskell [10] Richard Bird, Philip Wadler, 1991 4, ISBN4-7649-0181-1 Haskell Miranda [11] Haskell, 2006 3, ISBN4-8399-1962-3 [12] Haskell, 2006 6, ISBN4-7973-3602-1 III - 19