「プログラミング言語」 SICP 第4章 ~超言語的抽象~ その6

Similar documents
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


Level 3 Japanese (90570) 2011


浜松医科大学紀要

_Y05…X…`…‘…“†[…h…•


null element [...] An element which, in some particular description, is posited as existing at a certain point in a structure even though there is no


Page 1 of 6 B (The World of Mathematics) November 20, 2006 Final Exam 2006 Division: ID#: Name: 1. p, q, r (Let p, q, r are propositions. ) (10pts) (a

自分の天職をつかめ

process of understanding everyday language is similar, finally as far as word production is concerned, individual variations seem to be greater at an

第17回勉強会「英語の教え方教室」報告

126 学習院大学人文科学論集 ⅩⅩⅡ(2013) 1 2

Kyushu Communication Studies 第2号



I II


Corrections of the Results of Airborne Monitoring Surveys by MEXT and Ibaraki Prefecture

AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

<82E682B15F8E E696E6464>

Visual Evaluation of Polka-dot Patterns Yoojin LEE and Nobuko NARUSE * Granduate School of Bunka Women's University, and * Faculty of Fashion Science,

Holcombe Sidman & Tailby ABC A B B C B AA C


<8ED089EF8B D312D30914F95742E696E6464>

220 28;29) 30 35) 26;27) % 8.0% 9 36) 8) 14) 37) O O 13 2 E S % % 2 6 1fl 2fl 3fl 3 4

山陰研究横組み/論文廣嶋



井手友里子.indd

2 194

-2-

untitled

elemmay09.pub

Microsoft Word - j201drills27.doc

.N..

3_23.dvi

16_.....E...._.I.v2006



2


: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :



先端社会研究 ★5★号/4.山崎

FA

駒田朋子.indd

⑥中村 哲也(他).indd

Kansai University of Welfare Sciences Practical research on the effectiveness of the validation for the elderly with dementia Naoko Tsumura, Tomoko Mi

企業の信頼性を通じたブランド構築に関する考察


05_藤田先生_責

:. SPSS

Microsoft Word - ??? ????????? ????? 2013.docx

lagged behind social progress. During the wartime Chonaikai did cooperate with military activities. But it was not Chonaikai alone that cooperated. Al

鹿大広報149号


202

J.S

56 pp , 2005 * ******* *** ** CA CAMA

授受補助動詞の使用制限に与える敬語化の影響について : 「くださる」「いただく」を用いた感謝表現を中心に

Literacy 2 Mathematica Mathematica 3 Hiroshi Toyoizumi Univ. of Aizu REFERENCES [1] C.P Williams [2] [3] 1 Literacy 2 Mathematica Ma

国土技術政策総合研究所 研究資料

Microsoft Word - j201drills27.doc

Physical and Psychological Effects of Stressors in Female College Students Reizou Mita*1, Konosuke Tomabechi*1, Isao Yamaguchi*1, Naoko Soeno*1, Shuhe

Tab 5, 11 Tab 4, 10, Tab 3, 9, 15Tab 2, 8, 14 Tab 1, 7, 13 2


„h‹¤.05.07

Vol.54 No (July 2013) [9] [10] [11] [12], [13] 1 Fig. 1 Flowchart of the proposed system. c 2013 Information


16−ª1“ƒ-07‘¬ŠÑ

...



Microsoft Word - PCM TL-Ed.4.4(特定電気用品適合性検査申込のご案内)

A5 PDF.pwd

西川町広報誌NETWORKにしかわ2011年1月号

Ichiro KATO In the previous paper, The Tasks and Composition of the Public Fiscal 1 written by Ichiro Kato The Economic Journal of Taka

kut-paper-template2.dvi

h23w1.dvi

5. They made the right decision by electing him to lead ( 率いる リードする ) the group. a. show b. guide c. buy d. eat Exercise 2: What s the word? (5-7 minu


untitled


Core Ethics Vol.

Quiz 1 ID#: Name: 1. p, q, r (Let p, q and r be propositions. Determine whether the following equation holds or not by completing the truth table belo

9_89.pdf


28 Docker Design and Implementation of Program Evaluation System Using Docker Virtualized Environment

日本看護管理学会誌15-2

はじめに

< D8291BA2E706466>

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

2009 No


untitled

(CC Attribution) Lisp 2.1 (Gauche )

C. S2 X D. E.. (1) X S1 10 S2 X+S1 3 X+S S1S2 X+S1+S2 X S1 X+S S X+S2 X A. S1 2 a. b. c. d. e. 2

Transcription:

SICP 4 6 igarashi@kuis.kyoto-u.ac.jp July 21, 2015 ( ) SICP 4 ( 6) July 21, 2015 1 / 30

4.3: Variations on a Scheme Non-deterministic Computing 4.3.1: amb 4.3.2: 4.3.3: amb ( ) SICP 4 ( 6) July 21, 2015 2 / 30

Scheme def = ( ) generate and test ( ) ( ) ( ) SICP 4 ( 6) July 21, 2015 3 / 30

: : (define (prime-sum-pair list1 list2) (let ((a (an-element-of list1)) (b (an-element-of list2))) (require (prime? (+ a b))) (list a b))) an-element-of: ( ) require: ( ) SICP 4 ( 6) July 21, 2015 4 / 30

(prime-sum-pair (2 3 6) (4 5)) a b (prime? (+ a b)) 2 4 #f 2 5 #t (2 5) 3 4 #t (3 4) 3 5 #f 6 4 #f 6 5 #t (6 5) 3 ( ) SICP 4 ( 6) July 21, 2015 5 / 30

4.3.1: amb amb (amb e 1... e n ) e i n n = 0! ( ) ( ) SICP 4 ( 6) July 21, 2015 6 / 30

require an-element-of amb (define (require p) (if (not p) (amb))) (define (an-element-of items) (require (not (null? items)) (amb (car items) (an-element-of (cdr items))))) ;;! (define (an-integer-starting-from n) (amb n (an-integer-starting-from (+ n 1)))) ( ) SICP 4 ( 6) July 21, 2015 7 / 30

amb ( ) ((amb) ) ( ) SICP 4 ( 6) July 21, 2015 8 / 30

amb REPL try-again ( ) SICP 4 ( 6) July 21, 2015 9 / 30

4.3.2: ( ) SICP 4 ( 6) July 21, 2015 10 / 30

[Dinesman 1968] Baker, Cooper, Fletcher, Miller, Smith Baker Cooper Fletcher Miller Cooper Smith Fletcher Fletcher Cooper ( ) SICP 4 ( 6) July 21, 2015 11 / 30

(define (multiple-dwelling) (let ((baker (amb 1 2 3 4 5)) ;;... (smith (amb 1 2 3 4 5))) ;; (require (distinct? (list baker... smith))) (require (not (= baker 5))... (require (not (= (abs (- fletcher cooper)) 1))) ;; (list (list baker baker)... (list smith smith))))) ( ) SICP 4 ( 6) July 21, 2015 12 / 30

: : ::= student professor cat class ::= studies lectures eats sleeps ::= the a ::= ::= ( ) SICP 4 ( 6) July 21, 2015 13 / 30

(define articles (article the a)) ;; *unparsed* -- (define (parse-word word-list) (require (not (null? *unparsed*))) (require (memq (car *unparsed*) (cdr word-list))) (let ((found-word (car *unparsed*))) (set! *unparsed* (cdr *unparsed*)) (list (car word-list) found-word))) ( ) SICP 4 ( 6) July 21, 2015 14 / 30

(define (parse input) (set! *unparsed* input) (let ((sent (parse-sentence))) (require (null? *unparsed*)) sent)) ( ) SICP 4 ( 6) July 21, 2015 15 / 30 (define (parse-sentence) (list sentence (parse-noun-phrase) (parse-word verbs))) (define (parse-noun-phrase) (list noun-phrase (parse-word articles) (parse-word nouns)))

::=... ::= for to in by with ::= ::= ::= ::= ( ) SICP 4 ( 6) July 21, 2015 16 / 30

(define prepositions (prep for to in by with)) (define (parse-prepositional-phrase) (list prep-phrase (parse-word prepositions) (parse-noun-phrase))) (define (parse-sentence) (list sentence (parse-noun-phrase) (parse-verb-phrase))) ( ) SICP 4 ( 6) July 21, 2015 17 / 30

(define (parse-verb-phrase) (define (maybe-extend verb-phrase) (amb verb-phrase (maybe-extend (list verb-phrase verb-phrase (parse-prepositional-phrase))))) (maybe-extend (parse-word verbs))) ;; parse-noun-phrase ( ) SICP 4 ( 6) July 21, 2015 18 / 30

The student with the cat sleeps in the class. The professor lectures to the student with the cat. ( ) SICP 4 ( 6) July 21, 2015 19 / 30

Ex. 4.35 Write a procedure an-integer-between that returns an integer between two given bounds. This can be used to implement a procedure that finds Pythagorean triples, i.e., triples of integers (i, j, k) between the given bounds such that i < j and i 2 + j 2 = k 2, as follows: (define (a-pythagorean-triple-between lo hi) (let ((i (an-integer-between lo hi))) (let ((j (an-integer-between i hi))) (let ((k (an-integer-between j hi))) (require (= (+ (* i i) (* j j)) (* k k))) (list i j k))))) ( ) SICP 4 ( 6) July 21, 2015 20 / 30

Ex. 4.42: Five schoolgirls sat for an examination. Their parents so they thought showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters: Betty: Kitty was second in the examination. I was only third. Ethel: You ll be glad to hear that I was on top. Joan was second. Joan: I was third, and poor old Ethel was bottom. Kitty: I came out second. Mary was only fourth. Mary: I was fourth. Top place was taken by Betty. What was the order in which the five girls were placed? ( ) SICP 4 ( 6) July 21, 2015 21 / 30

Ex. 4.45 With the grammar given above, the following sentence can be parsed in five different ways: The professor lectures to the student in the class with the cat. Give the five parses and explain the differences in shades of meaning among them. ( ) SICP 4 ( 6) July 21, 2015 22 / 30

4.3: Variations on a Scheme Non-deterministic Computing 4.3.1: amb 4.3.2: 4.3.3: amb ( ) SICP 4 ( 6) July 21, 2015 23 / 30

amb catch/throw amb catch throw catch (catch ) amb ( ) SICP 4 ( 6) July 21, 2015 24 / 30

amb catch throw (throw...) (amb) (amb e1 e2): e1 (amb) e2 (amb (+ 2 (amb) 3) 4) 4 (amb) e1 ( ) SICP 4 ( 6) July 21, 2015 25 / 30

catch amb (let ((x (catch a 2))) (if (even? x) (throw a true) x)) (uncaught exception) (let ((x (amb 2 3))) (if (even? x) (amb) x)) 3 (amb...) (amb) (backtrack )! ( ) SICP 4 ( 6) July 21, 2015 26 / 30

throw (amb) TODO amb ( ) SICP 4 ( 6) July 21, 2015 27 / 30

amb : eval (amb) amb ( ) SICP 4 ( 6) July 21, 2015 28 / 30

SICP ch4-ambeval.scm: amb-eval.scm: require driver-loop ( ) SICP 4 ( 6) July 21, 2015 29 / 30

: evaluator ( ) SICP 4 ( 6) July 21, 2015 30 / 30