Download presentation
Presentation is loading. Please wait.
Published byCornelia Atkins Modified over 9 years ago
1
今日の内容 高階関数 関数を値として扱う 関数を引数にとる 関数を返す関数 プログラミングの例題 クイックソート
2
関数を値と扱う 関数を引数にとれる (define (f x) (x 10)) (define (f g ) ( g 10)) (f add1) 関数を値とする (define (g x) add1) g は add1 という関数を返す (注意:この場合 x は使われていない)
3
温度 C から F への変換 ;; convertCF : lon -> lon (define (convertCF alon) (cond [(empty? alon) empty] [else (cons (C->F (first alon)) (convertCF (rest alon)))])) 関数 C->F は自分で定義すること. 摂氏 (Celsius, ℃ ) 華氏( Fahrenheit, °F) F = 9/5 * C + 32
4
在庫品 (inventory-record) 在庫品は構造体として次のように定義する. (def-struct IR (name nitems price) name は品名 (シンボル) nitems はこの在庫品の在庫量 (数) price は価格 (数)
5
在庫品目名リスト ;; names : loIR -> los (define (names aloIR) (cond [(empty? aloIR) empty] [else (cons (IR-name (first aloIR)) (names (rest aloIR)))]))
6
プログラムの比較 1 (define (convertCF alon) (cond [(empty? alon) empty] [else (cons ( C->F (first alon)) (convertCF (rest alon)))])) (define (names aloIR) (cond [(empty? aloIR) empty] [else (cons (IR-name (first aloIR)) (names (rest aloIR)))]))
7
プログラムの比較 2 (define (convertCF G alon) (cond [(empty? alon) empty] [else (cons ( G (first alon)) (convertCF G (rest alon)))])) (define (names G aloIR) (cond [(empty? aloIR) empty] [else (cons ( G (first aloIR)) (names G (rest aloIR)))]))
8
プログラムの比較 3 (define (convertCF G x) (cond [(empty? x) empty] [else (cons ( G (first x)) (convertCF G (rest x)))])) (define (names G x) (cond [(empty? x) empty] [else (cons ( G (first x)) (names G (rest x)))]))
9
プログラムの比較 4 (define (g G x) (cond [(empty? x) empty] [else (cons ( G (first x)) (g G (rest x)))])) (define (names G x) (cond [(empty? x) empty] [else (cons ( G (first x)) (names G (rest x)))]))
10
map (define (map f x) (cond [(empty? x) empty] [else (cons (f (first x)) (map f (rest x)))]))
11
Map を用いた再定義 ;; convertCF-from-map : lon -> lon (define (convertCF-from-map alon) (map C->F alon)) ;; names-from-map : loIR -> los (define (names-from-map aloIR) (map IR-name aloIR))
12
多相型 (polymorphism) map : (number -> number) (listof number) -> (listof number) map : (IR -> symbol) (listof IR) -> (listof symbol) f : X -> Y map : (X -> Y) (listof X) -> (listof Y)
13
Filter (my-filter) (define (my-filter rel-op alon t) (cond [(empty? alon) empty] [(rel-op (first alon) t) (cons (first alon) (my-filter rel-op (rest alon) t))] [else (my-filter rel-op (rest alon) t)]))
14
Filter (my-filter) (define (my-filter rel-op alon t).... ) ;; (rel-op elem t) が true になる要素をリスト alon から ;; 取り出し,新たなリストを作る (my-filter > (list 3 -1 0 5 6 3) 0) => (list 3 5 6 3) (my-filter >= (list 3 -1 0 5 6 3) 0) => (list 3 0 5 6 3) (my-filter eq? (list 2 5 3 8 6 3) 3) => (list 3 3)
15
Filter(my-filter) を使う例: find ;; find : (listof IR) symbol -> (listof IR) (define (find aloir t) (define (eq-IR? ir p) (symbol=? (IR-name ir) p) (my-filter eq-IR? aloir t))
16
ラムダ式 関数を表す表現 : : = … | (lambda (... ) ) (lambda (x y) (+ (* x x) (* y y))) (lambda (x) x) (lambda (x y) x) (lambda (x y) (x y y))
17
ラムダ式の例 (lambda (x c) (> (* x x) c)) (lambda (ir p) (< (IR-price ir) p)) (lambda (ir p) (symbol=? (IR-name ir) p))
18
例 ((lambda (x y) (+ (* x y) 3)) 1 2) ((lambda (f x) (f x x)) + 10)
19
Quick sort の作成:ラムダ式を使 う例 ;; quick-sort : (listof number) -> (listof number) ;; to create a list of numbers with the same numbers as ;; alon sorted in ascending order (define (quick-sort alon) (cond [(empty? alon) empty] [else (append (quick-sort (smaller-items alon (first alon))) (list (first alon)) (quick-sort (larger-items alon (first alon))))]))
20
quick-sort の動き (quick-sort (list 3 4 2 6 5 1)) 3を取り出す 3 より小さい要素2,1 を選び出し (list 2 1) を作る ( quick-sort (list 2 1)) => (list 1 2) (list 3) を作る 3 より大きい要素4,6,5を選び出し (list 4 6 5) を 作る ( quick-sort (list 4 6 5)) => (list 4 5 6) (list 1 2) (list 3)(list 4 5 6) を結合 (append) する (list 1 2 3 4 5 6)
21
larger-items ;; larger-items : (listof number) number -> (listof number) ;; to create a list with all those numbers on alon ;; that are larger than threshold (define (larger-items alon threshold) (cond [(empty? alon) empty] [else (if (> (first alon) threshold) (cons (first alon) (larger-items (rest alon) threshold)) (larger-items (rest alon) threshold))]))
22
smaller-items ;; smaller-items : (listof number) number -> (listof number) ;; to create a list with all those numbers on alon ;; that are smaller than threshold (define (smaller-items alon threshold) (cond [(empty? alon) empty] [else (if (< (first alon) threshold) (cons (first alon) (smaller-items (rest alon) threshold)) (smaller-items (rest alon) threshold))])) my-filter を使うと簡単
23
システム組み込み関数 filter 注意 システム組み込み関数 filter がある. (filter rel-op alist) rel-op は一引数の述語 (真理値を返す関数) alist はリスト (filter even? (list 1 2 3))
24
larger-items, smaller-items の再定義 (define (larger-items alon threshold) (my-filter > alon threshold)) (define (smaller-items alon threshold) (my-filter < alon threshold))
25
filter を使った my-filter の再定義 (define (my-filter rel-op alon threshold) (filter (lambda (x) (rel-op x threshold)) alon))
26
最初の quick-sort の問題点 (quick-sort (list 3 2 2 5 5 6 1 3 2)) (list 1 2 2 2 3 3 5 5 6) が答えであってほしい (list 2 2 1 2) (list 3) (list 5 5 6) に分割されてし まう (quick-sort (list 3 2 2 5 5 6 1 3 2)) => (list 1 2 3 5 6) ここで,2,3,5,6がひとつになってし まう
27
正しい quick-sort (define (quick-sort alon) (cond [(empty? alon) empty] [else (append (quick-sort (not-larger-items (rest alon) (first alon))) (list (first alon)) (quick-sort (larger-items (rest alon) (first alon))))])) not-larger-items は自分で定義すること!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.