scheme
(define (is-prefix xs ys) (if (null? xs) #t (if (null? ys) #f (if (eq? (car xs) (car ys)) (is-prefix (cdr xs) (cdr ys)) #f)))) (define (match xs ys) (if (null? ys) #f (if (is-prefix xs ys) #t (match xs (cdr ys))))) (print (match '(3 5) '(1…
/usr/local/emacs/22.1/lispに以下のパッチをあてる。 強制保存と*scheme*バッファは3/10のウインドウが割り当てられる。 ファイル名を問い合わせるところはカレントバッファに固定。 $ diff -u cmuscheme.el.orig cmuscheme.el --- cmuscheme.el.orig 2007-…
M-x run-schemeを予めやっておかないとC-cC-lが有効じゃなかったので以下を書いておく。 (setq scheme-program-name "gosh -i") (autoload 'scheme-mode "cmuscheme" "Major mode for Scheme." t) (autoload 'run-scheme "cmuscheme" "Run an inferior Schem…
うーん、取りあえず作ってみた。 (define-syntax mymacro (syntax-rules () ((mymacro a1) (begin (print "last one.") (print a1))) ((mymacro a1 a2 ...) (begin (print a1) (mymacro a2 ...))))) (mymacro 1) (mymacro 1 2 3)実行結果は以下。 last one. …
なんかこんな感じで解きたかったのだがzip(list)が期待する動きと違った >>(use gauche.sequence) =>#<undef> >>(use util.combinations) =>#<undef> >>(filter (lambda (x) (eq? 9 (length (map car (group-sequence (sort (fold append () x))))))) (combinations (filte</undef></undef>…
よし、今までの研究成果を結集するのだ。 なんか (lambda (y) `(,(car y) ,@(bsort (cdr y)))) で呼び出しているbsortはクラインの壺みたいなイメージを持った。 すごくおもしろい。 >>(define bsort (lambda (x) (if (null? x) () ((lambda (y) `(,(car y) …
Exercise 1.12. The following pattern of numbers is called Pascal's triangle.The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 Write a procedure that computes el…
めずらしく英語のページを読む気になった。 Exercise 1.11. A function f is defined by the rule that f(n) = n if n 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative…
まだマクロが全然わかってないんだがとりあえずやってみたい気持ちが先行中(笑) ということで実装してみたのがこれ。 >>(define .. (lambda (x y) (let ((e (- y x -1))) (if (< e 0) () (iota e x))))) =>.. >>(.. 0 10) =>(0 1 2 3 4 5 6 7 8 9 10) >>(..…
まずは、shiroさんに教えてもらったGaucheのコード*1 (use util.match) (define qsort (match-lambda (()()) ((x . xs) (receive (l r) (partition (cut < <> x) xs) `(,@(qsort l) ,x ,@(qsort r))))))次になるべく似せたHaskellのコード import List qsort…
なんてことだ。gauche.sequence の group-sequenceがhaskellのgroupと同じだとは >>(use gauche.sequence) =>#<undef> >>(group-sequence '(1 1 2 3 3 2 2 4)) =>((1 1) (2) (3 3) (2 2) (4)) >>(map car (group-sequence '(1 1 2 3 3 2 2 4))) =>(1 2 3 2 4)あと、</undef>…
なるほど、カリー化とまでは行かないけど簡単に関数が作れるんだな。すごい。srfi-26 >>((lambda (x) (+ 1 2 x 4 5)) 6) =>18 >>((cut + 1 2 <> 4 5) 6) =>18 >>((cut + 1 2 <> 4 <>) 6 7) =>20 >>((cut <> 1 2 3 4 5) *) =>120よし、これでshiroさんのプロ…
しょうがないやるか。 >>(let ((x '(1 2 3 4))) `(a x c d)) =>(a x c d) >>(let ((x '(1 2 3 4))) `(a ,x c d)) =>(a (1 2 3 4) c d) >>(let ((x '(1 2 3 4))) `(a ,@x c d)) =>(a 1 2 3 4 c d) >>(let ((x 2)) `(a ,x c d)) =>(a 2 c d) >>(let ((x 2)) `(…
多値を使ったpartitionでqsortをしてみる >>(define qsort (lambda (x) (if (null? x) () (receive (l r) (partition (lambda (y) (< y (car x))) (cdr x)) (append (qsort l) (list (car x)) (qsort r)))))) =>qsort >>(qsort '(5 3 4 2 3)) =>(2 3 3 4 5)r…
filterとremoveを同時にやるpartitionを教えてもらったので使ってみようと思ったらなんと多値じゃないか。 なになに?参照渡しがどうのこうの・・・って schemeは参照渡しだよな。 >>(let ((a '(1 2 3))) (print a)((lambda (x) (filter! odd? x)) a) a) (1 …
とりあえずはこんなかんじらしい >> (match '(1 2 3) ((a b c) (list a b c))) => (1 2 3) >> (match '(1 (2 3)) ((a (b c)) (list a b c))) => (1 2 3)つぎに、リスト処理させてみる >>(match '(1 2 3 4) ((x . xs) (print x))) 1 =>#<undef> >>(match '(1 2 3 4) </undef>…
一つずらしたリストとzipして同じ要素のタプルを削除するフィルターを通す版 更に追記。zipつかうとちょっとだけ富豪な気分 srfi-1のzipは少ない方で作るので (cons () x)がちょっとだけかっこわるい。 >> (define uniq (lambda (x) (unzip1 (filter (lambda…
リストから任意のものを抜き出すのはrubyだとselectだからと思って探してみると srfi-1のfilterがそうだった。 >> (define qsort (lambda (x) (if (null? x) () (append (qsort (filter (lambda (y) (< y (car x))) (cdr x))) (list (car x)) (qsort (filter…
なるほどな。dorリストつかうのか。 >> ((lambda (x . y) (print x y)) 1) 1() => #<undef> >> ((lambda (x . y) (print x y)) 1 2) 1(2) => #<undef> >> ((lambda (x . y) (print x y)) 1 2 3) 1(2 3) => #<undef> >> ((lambda (x . y) (if (null? y) "only x" "x and y")) 1) =></undef></undef></undef>…
http://d.hatena.ne.jp/n9d/20071120/1195568232で、shiroさんに教えて頂いたのでもう少しやってみる。 gauche.sequenceを使う方法 >> (begin (use srfi-1) (use gauche.sequence) (take (shuffle (iota 10)) 4)) => (8 9 5 0) append-mapを使う方法 やばい…
util.combinationsを教えてもらったので富豪で再チャレンジ。 http://practical-scheme.net/gauche/man/gauche-refj_158.html#SEC430 何故かわからないが順列という名であるのにnPrでr=nのときしか用意されていない。 >> (permutations '(1 2 3)) => ((1 2 3…
まだまだだ。 乱数はsrfi-27番。 (use srfi-1) (use srfi-27) (random-source-randomize! default-random-source) (display (let me ((i (iota 10 0)) (j 4)) (if (> j 0) (let ((k (random-integer (length i)))) (cons (list-ref i k) (me (delete k i) (-…