>>90のMIT風の書き方の方 (cond->caseしただけですが) (define (make-shape newx newy) (let ( (x newx) (y newy)) (lambda (method parms) (case method ; accessors for x & y ((getx) x) ((gety) y) ((setx) (set! x parms)) ((sety) (set! y parms)) ; move the x & y coordinates ((moveto) (set! x (car parms)) (set! y (car (cdr parms)))) ((rmoveto) (set! x (+ x (car parms))) (set! y (+ y (car (cdr parms))))) (else (no-method)))))) letの辺りはよく分かりませんが、これをマクロでテンプレートにしたら 結構使えそうです。
scheme : (obj 'message param) or (class_message obj param) C dispatch(obj, message, param) or class_message(obj, param); C++ obj->message(param); or obj->receive(message, param); 考察 class_message形式や、 obj->messageは何を表してるのかわかりにくい。
構文の変換について質問なんですが、 (let ((a b) (c d)) e f) は ((lambda (a c) e f) b d) に書き換えられる事がわかったんですが、 ループ用の名前付きlet (let loop ((a b) (c d)) (loop e f) ) は ((lambda () (define (loop a c) (name e f)) (loop b d))) これよりもっと単純に置き換える事は可能でしょうか。 (define使わない方法とか)
185 名前: デフォルトの名無しさん 投稿日: 2001/05/15(火) 04:46
まちがえました ((lambda () (define (loop a c) (loop e f)) (loop b d)))
>>191の方法を使ったふぃぼなっち (define fib3 (lambda (n) ((lambda (fib-iter) (fib-iter fib-iter 1 0 n)) (lambda (fib-iter a b c) (if (= c 0) b (fib-iter fib-iter (+ a b) a (- c 1)) ) ))) ) (fib 30) =>832040
call/cc (call-with-current-continuation) の使い方その4 for文 (for init test incr body ...)
test body はwhileの時と同じ。initとincrが増えただけ。 initとincrにはfor内部で参照するカウンタ変数の初期化式と増減式を記述します。 initで(i 0) とすれば、iがカウンタ変数(初期値0)として使用されます。 initで宣言したカウンタ変数はtest/incr/body内で参照できます。
227 名前: 226 投稿日: 2001/05/23(水) 20:15
(続き) この例では、named-letの書式をそのまま当てはめて使用してるので、letと同じ 問題を含んでいます。(よって実際のC言語のfor文より使い勝手は落ちます。) initとincrに与える書式は、 init が () の時、 incr は () (==whileと同じ意味) init が (x 0) の時、 incr は (+ x 1) init が ((x 0) (y 1) (z 2)) の時 incr は ((+ x 1) y (+ z y)) を許します。 let束縛順序の規則が適用されるので、initに((x 0) (y x) (z y)) の様な式は書けません。 (被演算子側の x, yは外部に存在する物と解釈されます。)
(define-macro (do- body dmy test) (do-while-expand body dmy test))
281 名前: デフォルトの名無しさん 投稿日: 2001/06/10(日) 00:43
展開前 (define (example0 from to r p) (for (i from) (<= i to) (+ i 1) (if (< 0 r) (if (not (memv i p)) (example0 from to (- r 1) (cons i p))) (begin (disp-rev p)(display " ") (break) ))))
展開後 (define (example0 from to r p) (call/cc (lambda (break) (let G22 ((i from)) (cond ((<= i to) (call/cc (lambda (continue) (if (< 0 r) (if (not (memv i p)) (example0 from to (- r 1) (cons i p))) (begin (disp-rev p)(display " ") (break))))) (G22 (+ i 1))))))))
>>286 そのレスのC言語での解答(かわいそうなので) #include <stdio.h> #include <stdlib.h> #include <assert.h> int memv_int(int k, int *a, size_t an) { int i = 0; for (i = 0; i < an; i++) if (k == a[i]) return 1; return 0; }
void disp(int *a, size_t n) { int i; for (i = 0 ; i < n; i++) printf("%d", a[i]); }
void example0(int n, int r, int *a, size_t an) { int i; for (i = 1; i <= n; i++) if (r) { if (!memv_int(i, a, an)) { a[an++] = i; example0(n, r - 1, a, an); an--; } } else { disp(a, an); printf(" "); break; } }
void example(int n, int r) { int *a = malloc(sizeof(int) * r); assert(a); example0(n, r, a, 0); free(a); }
This is very deep material for a programming newbie to learn outside a course, but for an experienced nerd who's looking for a systematic framework, it's absolutely terrific. I had done lots of lisp and compiler work before reading the book, so many of the concepts were not new. But it's with this framework in mind that I learn new technologies, and this approach greatly speeds up how long it takes to understand each week's "new" hot product/language/tool/mindset. Put another way: why do so many popular computer books take 1000 pages to describe a few trivial concepts?
例えば、 (a + b) * c を前置式で入力するとき、 (+ a b) って入力してから、「あ”、*あった・・・。欝だ」みたいな感じです。 このとき、カーソル左を連打して、忘れていたのを挿入してます。 (↑ここが、エディタのモードを使えばなんとかなるかな?と思った) (例えば、M-x chotto-modoru(仮)) (Emacsに(たぶん)schemeモードとかあると思うけど) Winではxyzzy使ってるし・・・。
「As far as Lisp is concerned, the words we have been using in the lists cannot be divided into any smaller parts and still mean the same thing as part of a program; likewise with numbers and single character symbols like `+'. 」
>>511 Schemeの設計された年代を考えると、 並列化scheme方言の事まで視野に入れたと考えたとか、 研究テーマとしてcompilerのoptimizeを追求したかったという事も考えられるけど、 SICPなんか見るとやっぱりWhat you see is What the program meansって、 考えがあると思います。暗黙の評価順序はそういう考えに反するのでしょう。
Side effect の日本語訳が 副作用 になっていますが、「クスリの副作用」など、あまり良いイメージがありません。 そこで、 「2次効果」 と改訳したいと思いますが?
522 名前: 俺様訳 投稿日: 2001/07/31(火) 14:15
「If evaluation applies to a list that is inside another list, the outer list may use the value returned by the first evaluation as information when the outer list is evaluated. 」
>>612 のmatch-letを使った条件式の変形 (define (condition-optimize x) (or (match-let '(test e) '(if test e #f) x (list 'and test e) ) (match-let '(test e) '(if test #t e) x (list 'or test e) ) (match-let '(test t f) '(if (not test) t f) x (condition-optimize (list 'if test f t)) ) ; ... x ))
(condition-optimize '(if (not a) b c)) =>(if a c b) (condition-optimize '(if a b #f)) =>(and a b) >(condition-optimize '(if a #t b)) =>(or a b) (condition-optimize '(if (not a) #f b)) =>(and a b)
I can't wake up, in the morning Cause of what I've been doing for most of the night. Teacher don't you know my program is done? And girls just wanna defun.
The phone rings, in the middle of the night Advisor screams, "Watcha gonna do with your life?" Patrick**, how I relish double-oh-one***! And girls just wanna defun.
They just wanna, just wanna, yeah Girls just wanna defun.
Some people say A beautiful girl can't tool all night like The rest of the world. I wanna be the one to welcome the sun. And girls just wanna defun.