# 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

Similar documents
Parametric Polymorphism

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

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

Jacques Garrigue

Copyright c 2006 Zhenjiang Hu, All Right Reserved.

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

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

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

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

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

Objective Caml 3.12 Jacques Garrigue ( ) with Alain Frisch (Lexifi), OCaml developper team (INRIA)

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

jssst-ocaml.mgp

y = x 4 y = x 8 3 y = x 4 y = x 3. 4 f(x) = x y = f(x) 4 x =,, 3, 4, 5 5 f(x) f() = f() = 3 f(3) = 3 4 f(4) = 4 *3 S S = f() + f() + f(3) + f(4) () *4

コンピュータ概論

, 1 ( f n (x))dx d dx ( f n (x)) 1 f n (x)dx d dx f n(x) lim f n (x) = [, 1] x f n (x) = n x x 1 f n (x) = x f n (x) = x 1 x n n f n(x) = [, 1] f n (x

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

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

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

- II

Java演習(4) -- 変数と型 --


Microsoft PowerPoint - IntroAlgDs-05-5.ppt

コンパイラ演習 第 7 回

syspro-0405.ppt

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.

ML 演習 第 4 回

プログラミングD - Java

joho09.ppt

() Remrk I = [0, ] [x i, x i ]. (x : ) f(x) = 0 (x : ) ξ i, (f) = f(ξ i )(x i x i ) = (x i x i ) = ξ i, (f) = f(ξ i )(x i x i ) = 0 (f) 0.

x A Aω ẋ ẋ 2 + ω 2 x 2 = ω 2 A 2. (ẋ, ωx) ζ ẋ + iωx ζ ζ dζ = ẍ + iωẋ = ẍ + iω(ζ iωx) dt dζ dt iωζ = ẍ + ω2 x (2.1) ζ ζ = Aωe iωt = Aω cos ωt + iaω sin

bc0710_010_015.indd

all.dvi

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

For_Beginners_CAPL.indd

°ÌÁê¿ô³ØII

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 (..

listings-ext

1 I 1.1 ± e = = - = C C MKSA [m], [Kg] [s] [A] 1C 1A 1 MKSA 1C 1C +q q +q q 1

x () g(x) = f(t) dt f(x), F (x) 3x () g(x) g (x) f(x), F (x) (3) h(x) = x 3x tf(t) dt.9 = {(x, y) ; x, y, x + y } f(x, y) = xy( x y). h (x) f(x), F (x

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

( ) kadai4, kadai4.zip.,. 3 cos x [ π, π] Python. ( 100 ), x cos x ( ). (, ). def print cos(): print cos()

fp.gby

25 II :30 16:00 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 3.. Answer the following 3 proble

My関数の作成演習問題集

1 c Koichi Suga, ISBN

2

untitled

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in

ex01.dvi

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

ex01.dvi

Collatzの問題 (数学/数理科学セレクト1)

Microsoft PowerPoint - PC2007.ppt

2.2 h h l L h L = l cot h (1) (1) L l L l l = L tan h (2) (2) L l 2 l 3 h 2.3 a h a h (a, h)


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

プログラミング言語 8 字句解析器(lexer)と構文解析器(parser)

haskell.gby

平成 19 年度 ( 第 29 回 ) 数学入門公開講座テキスト ( 京都大学数理解析研究所, 平成 19 ~8 年月 72 月日開催 30 日 ) 1 PCF (Programming language for Computable Functions) PCF adequacy adequacy

body.dvi

応用数学特論.dvi

2012 IA 8 I p.3, 2 p.19, 3 p.19, 4 p.22, 5 p.27, 6 p.27, 7 p

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裱£²²ó ¡Ý½ÉÂꣲ¤Î²òÀ⡤±é½¬£²¡Ý

Microsoft Word - 03-数値計算の基礎.docx

基礎数学I

[ ] x f(x) F = f(x) F(x) f(x) f(x) f(x)dx A p.2/29

aisatu.pdf

Sigma

Sigma

( ) ( ) 1729 (, 2016:17) = = (1) 1 1

ML 演習 第 4 回

プログラミングD - Java

2.2 Sage I 11 factor Sage Sage exit quit 1 sage : exit 2 Exiting Sage ( CPU time 0m0.06s, Wall time 2m8.71 s). 2.2 Sage Python Sage 1. Sage.sage 2. sa

untitled

( 28 ) ( ) ( ) 0 This note is c 2016, 2017 by Setsuo Taniguchi. It may be used for personal or classroom purposes, but not for commercial purp

Microsoft PowerPoint - IntroAlgDs-07-6.ppt [互換モード]

Excel ではじめる数値解析 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 初版 1 刷発行時のものです.

94 expression True False expression FalseMSDN IsNumber WorksheetFunctionIsNumberexpression expression True Office support.office.com/ja-jp/ S

80 X 1, X 2,, X n ( λ ) λ P(X = x) = f (x; λ) = λx e λ, x = 0, 1, 2, x! l(λ) = n f (x i ; λ) = i=1 i=1 n λ x i e λ i=1 x i! = λ n i=1 x i e nλ n i=1 x

解きながら学ぶJava入門編

³ÎΨÏÀ

ohp03.dvi

f(x) x S (optimal solution) f(x ) (optimal value) f(x) (1) 3 GLPK glpsol -m -d -m glpsol -h -m -d -o -y --simplex ( ) --interior --min --max --check -

x h = (b a)/n [x i, x i+1 ] = [a+i h, a+ (i + 1) h] A(x i ) A(x i ) = h 2 {f(x i) + f(x i+1 ) = h {f(a + i h) + f(a + (i + 1) h), (2) 2 a b n A(x i )

Chap11.dvi

18 ( ) I II III A B C(100 ) 1, 2, 3, 5 I II A B (100 ) 1, 2, 3 I II A B (80 ) 6 8 I II III A B C(80 ) 1 n (1 + x) n (1) n C 1 + n C

平成 28 年度 ( 第 38 回 ) 数学入門公開講座テキスト ( 京都大学数理解析研究所, 平成 ~8 28 月年 48 日開催月 1 日 semantics FB 1 x, y, z,... FB 1. FB (Boolean) Functional

,. Black-Scholes u t t, x c u 0 t, x x u t t, x c u t, x x u t t, x + σ x u t, x + rx ut, x rux, t 0 x x,,.,. Step 3, 7,,, Step 6., Step 4,. Step 5,,.

2

こんにちは由美子です

1 1.1 ( ). z = a + bi, a, b R 0 a, b 0 a 2 + b 2 0 z = a + bi = ( ) a 2 + b 2 a a 2 + b + b 2 a 2 + b i 2 r = a 2 + b 2 θ cos θ = a a 2 + b 2, sin θ =

K227 Java 2

() x + y + y + x dy dx = 0 () dy + xy = x dx y + x y ( 5) ( s55906) 0.7. (). 5 (). ( 6) ( s6590) 0.8 m n. 0.9 n n A. ( 6) ( s6590) f A (λ) = det(a λi)

r03.dvi

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

橡00扉.PDF

Dive into Algebraic Effects

4 R f(x)dx = f(z) f(z) R f(z) = lim R f(x) p(x) q(x) f(x) = p(x) q(x) = [ q(x) [ p(x) + p(x) [ q(x) dx =πi Res(z ) + Res(z )+ + Res(z n ) Res(z k ) k

Transcription:

II 4 : 2001 11 7 keywords: 1 OCaml OCaml (first-class value) (higher-order function) 1.1 1 2 + 2 2 + + n 2 sqsum 1 3 + 2 3 + + n 3 cbsum # let rec sqsum n = # if n = 0 then 0 else n * n + sqsum (n - 1) # let rec cbsum n = # if n = 0 then 0 else n * n * n + cbsum (n - 1);; val sqsum : int -> int = <fun> val cbsum : int -> int = <fun> n 0 0 n n 1

# 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 cbsum # let square x = x * x # let sqsum n = sigma (square, n) # let cbsum n = # let cube x = x * x * x in sigma (cube, n);; val square : int -> int = <fun> val sqsum : int -> int = <fun> val cbsum : int -> int = <fun> cbsum let- # sqsum 5;; - : int = 55 # cbsum 5;; - : int = 225 1.2 n sigma f(i) f i=0 let f (anonymous function) OCaml OCaml fun -> e e fun # let cbsum n = sigma ((fun x -> x * x * x), n);; val cbsum : int -> int = <fun> # let sq5 = ((fun x -> x * x), 5) in # sigma sq5;; - : int = 55 # (fun x -> x * x) 7;; - : int = 49 2

( ) fun () (, n x * x * x n fun () ) let let f x = e let f = fun x -> e (fun) 1.3 OCaml 1 Haskell Curry (curried function) x y e x y e s 1, s 2 s 1 s 2 # let concat (s1, s2) = s1 ^ s2 ;; val concat : string * string -> string = <fun> ( ) concat ("abc", "def") # let concat_curry s1 = fun s2 -> s1 ^ s2;; val concat_curry : string -> string -> string = <fun> concat_curry fun s2 ( ) s1 concat_curry string -> (string -> string) 2 # (concat_curry "abc") "def";; - : string = "abcdef" 3

(...) abc (...) "def" (partial application) Mr. ( ) # let add_title = concat_curry "Mr. ";; val add_title : string -> string = <fun> # add_title "Igarashi";; - : string = "Mr. Igarashi" add_title fun s2 -> "Mr. " ^ s2 fun OCaml fun let fun # let concat_curry s1 s2 = s1 ^ s2;; val concat_curry : string -> string -> string = <fun> # let concat_curry = fun s1 s2 -> s1 ^ s2;; val concat_curry : string -> string -> string = <fun> fun 1 -> fun 2 ->... fun n -> e fun 1 2... n -> e (let ) (((f x) y) z) f x y z ( ) t1 -> t2 -> t3 -> t4 t1 -> (t2 -> (t3 -> t4)) 4

/ +, ^ (infix operator) (int->int->int ) OCaml ( ) + ( ) ( ) # (+);; - : int -> int -> int = <fun> # ( * ) 2 3;; - : int = 6 * / mod, *, =, or, & 1 =, <, >, @, ^,, &, +, -, *, /, $, % 2!, $, %, *, +, -,., /, :, <, =, >,?, @, ^, ~ ( ) # let (^-^) x y = x * 2 + y * 3;; val ( ^-^ ) : int -> int -> int = <fun> # 9 ^-^ 6;; - : int = 36 () # let (!! ) x = x + 1;; val (!! ) : int -> int = <fun> #!!;; Characters 2-4: Syntax error # (!!);; - : int -> int = <fun> #!! 3;; - : int = 4 -, -. 1!,?, ~ 2!, $, %, *, +, -,., /, :, <, =, >,?, @, ^, ~ 1 5

1: -, -. ** *, /, %, mod +, - :: @ ^ = <, not &, && or,, <-, := if ; let, match, fun, function, try 6

1.4 Case Study: Newton-Raphson Newton-Raphson Newton-Raphson f f(x) = 0 g(x) = x f(x) f (x) (g(a) = a a) ( x n = x n 1 f(x n 1 )/f (x n 1 ) ) dx g (x) = g(x + dx) g(x) dx # let deriv f = # let dx = 0.1e-10 in # fun x -> (f(x +. dx) -. f(x)) /. dx;; val deriv : (float -> float) -> float -> float = <fun> f(x) = x 3 3 # deriv (fun x -> x *. x *. x) 3.0;; - : float = 26.9999134161 f x f(x), f(f(x)),... f n (x) = f n 1 (x) f n (x) f n 1 (x) f n (x) # let fixpoint f init = # (* computes a fixed-point of f, i.e., r such that f(r)=r *) # let threshold = 0.1e-10 in # let rec loop x = # let next = f x in # if abs_float (x -. next) < threshold then x # else loop next # in loop init;; val fixpoint : (float -> float) -> float -> float = <fun> Newton-Raphson # let newton_transform f = fun x -> x -. f(x) /. (deriv f x);; val newton_transform : (float -> float) -> float -> float = <fun> 7

Newton-Raphson f(x) = 0 f guess # let newton_method f guess = fixpoint (newton_transform f) guess;; val newton_method : (float -> float) -> float -> float = <fun> # let square_root x = newton_method (fun y -> y *. y -. x) 1.0;; val square_root : float -> float = <fun> # square_root 5.0;; - : float = 2.2360679775 1.5 b Exercise 4.1 f f(x)dx integral f a b π sin xdx 0 a b a n δ i (f (a + (i 1) δ) + f (a + iδ)) δ 2 Exercise 4.2 3.8 pow (pow n x) 3 cube (pow x n) cube pow? Exercise 4.3 fun? Exercise 4.4 3 int -> int -> int -> int (int -> int) -> int -> int (int -> int -> int) -> int 8

2 int * int # let fstint ((x, y) : int * int) = x;; val fstint : int * int -> int = <fun> (int * float) * string # let fst_ifs ((x, y) : (int * float) * string) = x;; val fst_ifs : (int * float) * string -> int * float = <fun>??? (int int * float string) # let fst ((x, y) : a * b) = x;; val fst : a * b -> a = <fun> a b (type variable) (polymorphic function) fst a * b -> a T1, T2 T1 * T2 -> T1 ( a. b.( a * b -> a) ) OCaml # let fst (x, y) = x;; val fst : a * b -> a = <fun> ( (principal type) ) fst a * b -> a ( ) 9

# fst (2, 3);; - : int = 2 # fst ((4, 5.2), "foo");; - : int * float = 4, 5.2 a, b int a int * float b string fst int * int -> int (int * float) * string -> int * float (polymorphism) (parametric polymorphism) ( ) id (identity function) apply # let id x = x;; val id : a -> a = <fun> # let apply f x = f x;; val apply : ( a -> b) -> a -> b = <fun> apply ( a -> b) f a x a f x f x # apply abs (-5);; - : int = 5 # apply abs "baz";; Characters 6-9: This expression has type int -> int but is here used with type string -> a? fst (x, y) x, y apply f x ( ) a * b -> a + (adhoc polymorphism) 10

(subtyping polymorphism) 2.1 let OCaml let( ) x (id ) # (fun x -> (x 1, x 2.0)) id;; Characters 19-22: This expression has type float but is here used with type int x 1 x int float let f x double # let double f x = f (f x);; val double : ( a -> a) -> a -> a = <fun> # double (fun x -> x + 1) 3;; - : int = 5 # double (fun s -> "<" ^ s ^ ">") "abc";; - : string = "<<abc>>" 4 # double double (fun x -> x + 1) 3;; - : int = 7 # double double (fun s -> "<" ^ s ^ ">") "abc";; - : string = "<<<<abc>>>>" let # let fourtimes = double double;; val fourtimes : ( _a -> _a) -> _a -> _a = <fun> _a 11

# fourtimes (fun x -> x + 1) 3;; - : int = 7 # fourtimes;; - : (int -> int) -> int -> int = <fun> # fourtimes (fun s -> "<" ^ s ^ ">") "abc";; Characters 35-40: This expression has type string but is here used with type int fourtimes int (value polymorphism) ( ) fourtimes # let fourtimes f = double double f # (* equivalent to "let fourtimes = fun f -> double double f" *) ;; val fourtimes : ( a -> a) -> a -> a = <fun> ( fun ) # fourtimes (fun x -> x + 1) 3;; - : int = 7 # fourtimes (fun s -> "<" ^ s ^ ">") "abc";; - : string = "<<<<abc>>>>" 2.2 OCaml fun x -> x + 1 int -> int + int -> int -> int int x + x int 1 int OK x int x + 1 int x + 1 int x int -> int OCaml 1 12

if ( x a int ) fun x -> if x then 1 else x if if bool then else x a int = a = bool let ( ) apply f, x a, b f x f x c a = b -> c fun f x -> f x a -> b -> c ( b -> c) -> b -> c / / Newton-Raphson let square_root x = newton_method (fun y -> y *. y -. x) 1.0;; newton_method newton_method y float fun (y : float) ->... fst # let fst ((x, y) : a * a) = x;; val fst : a * a -> a = <fun> # fst (true, 3.2);; Characters 6-15: This expression has type bool * float but is here used with type bool * bool 2.3 Case Study: CPU 13

λ- λ- (OCaml fun ) λ- o OCaml # let o f g x = f (g x);; val o : ( a -> b) -> ( c -> a) -> c -> b = <fun> fun x -> -. (sqrt x) # o ( -. ) sqrt;; - : float -> float -> float = <fun> id ( I ) id f f I # o id (sqrt) 3.0;; - : float = 1.73205080757 K # let k x y = x;; val k : a -> b -> a = <fun> k x x # let const17 = k 17 in const17 4.0;; - : int = 17 S # let s x y z = x z (y z);; val s : ( a -> b -> c) -> ( a -> b) -> a -> c = <fun> OCaml fun (id, double λ- ) S K I S K K # s k k 5;; - : int = 5 λ- 14

2.4 Exercise 4.5 curry curry (2 ) uncurry # let curry f x y = f (x, y);; val curry : ( a * b -> c) -> a -> b -> c = <fun> Exercise 4.6 repeat double, fourtimes f n x # let rec repeat f n x = # if n > 0 then repeat f (n - 1) (f x) else x;; val repeat : ( a -> a) -> int -> a -> a = <fun> fib... let fib n = let (fibn, _) =... in fibn;; Exercise 4.7 funny # (* f <@> g denotes the composition of f and g *) # let ( <@> ) f g x = (f (g x)) ;; val ( <@> ) : ( a -> b) -> ( c -> a) -> c -> b = <fun> # let rec funny f n = # if n = 0 then id # else if n mod 2 = 0 then funny (f <@> f) (n / 2) # else funny (f <@> f) (n / 2) <@> f;; val funny : ( a -> a) -> int -> a -> a = <fun> Exercise 4.8 S K K S K K 1 Exercise 4.9 double double f x f (f (f (f x))) 15