PowerPoint プレゼンテーション

Download Report

Transcript PowerPoint プレゼンテーション

今日の内容


相互参照(再帰的)データ構造
局所定義
 局所変数、局所関数
リスト(list)と構造(structure)の混じった例

家系図の定義と処理

先週とは,リンクが逆向き


先週は,父と母にリンクがあった
今週は,子供の(リスト)へのリンクがある
再帰的な構造:家系図
Carl (1926)
green
Adam (1950)
yeloow
Bettina (1926)
green
Dave(1955)
black
Eva (1965)
blue
Fred (1966)
pink
Gustav (1988)
brown
Parentのデータ構造の定義

parent は構造:

(define-struct parent (loc name date eyes))





locはchildrenのリスト
nameは名前
dateは生年
eyesは目の色
(make-parent aloc aname adate aneye)



aloc はchildrenのリスト
aname と aneye は シンボル
adate は数
Childrenのリストの定義

childrenのリスト


empty
(cons p loc)


p は parent
loc は childrenのリスト
相互参照定義

parent は以下の構造

(make-parent aloc n d e)


aloc はchildrenのリスト, n と e は シンボル, d は数
A list of children


empty
(cons p aloc)


p は parent
aloc は childrenのリスト
相互参照:プログラムの基本構造

相互再帰的な関数
(define (func-on-parent p)
;; 家系図に対する関数
… (func-on-childlen (parent-loc p))…))
(define (func-on-children aloc)
;; 家系図のリストに対する関数
(cond
[(empty? aloc) … ]
[else
… (func-on-parent (first aloc)) …
… (func-on-children (rest aloc))…]))
blue-eyed-descendant? : ftn -> boolean
; blue-eyed-descendant? : ftn (family tree node) -> boolean
;; parent あるいはその子孫が青い目であるか判定する
(define (blue-eyed-descendant? aparent) ...)
答えの例
(blue-eyed-descendant? Gustav)
= false
(blue-eyed-descendant? Eva)
= true
(blue-eyed-descendant? Bettina)
= true
blue-eyed-descendant? のプログラム
;; blue-eyed-descendant? : ftn -> boolean
;; parent あるいはその子孫が青い目であるか判定する
(define (blue-eyed-descendant? aparent)
(cond
[(symbol=? (parent-eyes aparent) 'blue) true]
[else (blue-eyed-children? (parent-loc aparent))]))
blue-eyed-children?
;; blue-eyed-children? : list-of-children -> boolean
;; alocに含まれるparentが青い目かその子孫が青い目が
;; を判定する
(define (blue-eyed-children? aloc)
(cond
[(empty? aloc) false]
[else
(cond
[(blue-eyed-descendant? (first aloc)) true]
[else (blue-eyed-children? (rest aloc))])]))
blue-eyed-descendant?の見直し
(define (blue-eyed-descendant? aparent)
(or (symbol=? (parent-eyes aparent) 'blue)
(blue-eyed-children?
(parent-loc aparent))))
blue-eyed-children? の見直し
(define (blue-eyed-children? aloc)
(cond
[(empty? aloc) false]
[else (or (blue-eyed-descendant? (first aloc))
(blue-eyed-children? (rest aloc)))]))
Web ページに関する問題


Web に、ある特定の記号が現れるかを調べる
関数 occurs を定義する
Web ページの定義

Web ページは次のように定義される構造



(define-struct wp (header body))
ここで,headerはシンボルでbodyは(Webの)ドキュメ
ント
ドキュメントは


empty
(cons s p)


s はシンボルで p はドキュメント
(cons w p)

w は Web ページ で p はドキュメント
Web ページの例


Webページ: (make-wp シンボル ドキュメント)
ドキュメント: empty
(list Item …. Item)



Item: シンボル あるいはWebページ
(make-wp ‘a (list ‘b ‘x))
(make-wp ‘a
(list (make-wp ‘c empty)
(make-wp ‘d (list (make-wp ‘a
(list ’b ‘x))))))
Webページ:プログラムの基本構造
(define (func-on-wp wp)
;; ウェブページに対する関数
… (func-on-doc (wp-body wp))…))
(define (func-on-doc adoc)
;; ドキュメントに対する関数
(cond
[(empty? adoc) … ]
[(symbol? (frist adoc)) … (func-on-doc (rest adoc)) …]
[ else
… (func-on-wp (first adoc)) …
… (func-on-doc (rest adoc))…]))
occur
; web ページを定義する
(define-struct wp (header body))
; web ページ awp に記号asymbが現れるか
; どうかを調べる
(define (occurs asymb awp)
(or (symbol=? asymb (wp-header awp))
(occurs-webdoc asymb (wp-body awp))))
occurs-webdoc
; webドキュメント awebdoc にシンボルasymbが現れる
; かどうかを調べる
(define (occurs-webdoc asymb awebdoc)
(cond
[(empty? awebdoc) false]
[(symbol? (first awebdoc))
(or (symbol=? (first awebdoc) asymb)
(occurs-webdoc asymb (rest awebdoc)))]
[else (or
(occurs asymb (first awebdoc))
(occurs-webdoc asymb (rest awebdoc)))]))
局所変数

(let ((<変数> <式>) … (<変数> <式>)) <式>)
 <変数> は,<式>の中でのみ有効

例: f(x,y) = x(1 + xy)2+y(1 – y) + (1 + xy)(1- y)

a = 1 + xy
b=1–y

f (x,y) = xa2 + yb + ab

局所変数

例: f(x,y) = x(1 + xy)2+y(1 – y) + (1 + xy)(1- y)




a = 1 + xy
b=1–y
f (x,y) = xa2 + yb + ab
プログラム
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x a a) (* y b) (* a b))))
局所関数の定義

関数定義の中で,defineを用いて局所関数を定
義できる
(define (f2 x y)
(define (f-helper a b)
(+ (* x a a) (* y b) (* a b)))
(f-helper (+ 1 (* x y)) (- 1 y)))
局所的な相互再帰関数の定義
(define (myodd? an)
(define (odd-helper? an)
(cond
[(zero? an) false]
[else (even-helper? (sub1 an))]))
(define (even-helper? an)
(cond
[(zero? an) true]
[else (odd-helper? (sub1 an))]))
(odd-helper? an))
局所定義による単純挿入法
(define (sort alon)
(cond
[(empty? alon) empty]
[(cons? alon) (insert (first alon) (sort (rest alon)))]))
(define (insert an alon)
(cond
[(empty? alon) (list an)]
[else (cond
[(> an (first alon)) (cons an alon)]
[else (cons (first alon) (insert an (rest alon)))])]))
局所関数を用いたsortの再定義
(define (sort2 alon)
(define (insert an alon)
(cond
[(empty? alon) (list an)]
[else (cond
[(> an (first alon)) (cons an alon)]
[else (cons (first alon) (insert an (rest alon)))])]))
(cond
[(empty? alon) empty]
[(cons? alon) (insert (first alon) (sort (rest alon)))]))
問題1:プログラムの基本構造

相互再帰的な関数
(define (func-on-tree t)
;; 木に対する関数
… (tree-label t)…
… (func-on-childlen (tree-children t))…))
(define (func-on-children ts)
;; 木のリストに対する関数
(cond
[(empty? ts) … ]
[else
… (func-on-tree (first ts)) …
… (func-on-children (rest ts))…]))