← cheatsheet

$ cat ~/cheatsheet/scheme.md

Scheme / Gauche チートシート

Scheme (R7RS) と Gauche のよく使う構文・手続き、gosh の起動オプションの早見表。

端末からは curl callcc.me/cheat/scheme でも読めます

Gauche 0.9.10 で確かめています。

gosh の起動

コマンド動作
goshREPL を起動する
gosh file.scmスクリプトを実行する (main があれば呼ぶ)
gosh -e '(print 1)' -E exit式を評価してから終わる
gosh -E 'print "hi"' -E exit-E は式を括弧で囲んで読む
gosh -u gauche.processモジュールを読み込んでから起動する
gosh -l lib.scmファイルを読み込んでから起動する
gosh -I dir / gosh -A dirロードパスの先頭 / 末尾に dir を加える
gosh -r7R7RS のモードで起動する
gosh -Vバージョンを表示する

スクリプトの形:

#!/usr/bin/env gosh
(define (main args)          ; args の先頭はスクリプトの名前
  (print "hello, " (cadr args))
  0)                         ; 終了コード

定義と束縛

書き方意味
(define x 1)変数を定義する
(define (f a b) ...)手続きを定義する
(lambda (x . rest) ...)無名の手続き (rest に残りの引数のリスト)
(let ((a 1) (b 2)) ...)局所変数
(let* ((a 1) (b (+ a 1))) ...)前の変数を使える let
(let loop ((i 0)) (when (< i 3) (loop (+ i 1))))名前付き let (ループ)
(set! x 2)代入
(receive (q r) (quotient&remainder 7 2) ...)多値を受け取る (Gauche)

条件

書き方意味
(if c a b)条件分岐
(cond (c1 a) (c2 b) (else z))複数の条件
(case x ((1 2) 'small) (else 'big))値で分ける (eqv? で比べる)
(when c ...) / (unless c ...)片側だけの分岐
(and ...) / (or ...)論理演算 (最後に評価した値を返す)

リスト

手続き例 → 結果
cons car cdr(cons 1 '(2)) → (1 2)
list length append reverse(append '(1) '(2 3)) → (1 2 3)
list-ref(list-ref '(a b c) 1) → b
map for-each(map + '(1 2) '(10 20)) → (11 22)
filter remove(filter odd? '(1 2 3)) → (1 3)
fold(fold + 0 '(1 2 3)) → 6
assoc assq(assq 'b '((a . 1) (b . 2))) → (b . 2)
iota(iota 3) → (0 1 2)
sort(sort '(3 1 2) <) → (1 2 3)

関数合成と部分適用 (Gauche)

手続き例 → 結果
compose .$ (右の手続きから順に適用)((.$ (cut * <> 2) (cut + <> 1)) 3) → 8
compose((compose car cdr) '(1 2 3)) → 2
pa$ (部分適用)((pa$ + 10) 5) → 15
cut cute (<> に引数が入る)(map (cut * 2 <>) '(1 2 3)) → (2 4 6)
$ (入れ子の呼び出しを平らに書く)($ list 1 $ + 2 3) → (1 5)
complement((complement odd?) 3) → #f
every-pred any-pred((every-pred number? positive?) 5) → #t
map$ filter$ apply$((map$ (pa$ * 2)) '(1 2 3)) → (2 4 6)
identity(identity 5) → 5

文字列と数

手続き例 → 結果
string-append(string-append "a" "b") → "ab"
string-length substring(substring "hello" 1 3) → "el"
string-split (Gauche)(string-split "a,b" #\,) → ("a" "b")
string-join (Gauche)(string-join '("a" "b") ",") → "a,b"
number->string string->number(string->number "ff" 16) → 255
x->string (Gauche)(x->string 'sym) → "sym"
format(format #f "~a = ~s" 'x "y") → "x = \"y\""

ハッシュテーブル (Gauche)

(define h (make-hash-table 'equal?))
(hash-table-put! h "key" 1)
(hash-table-get h "key" #f)        ; => 1 (無ければ #f)
(hash-table-update! h "key" (cut + <> 1) 0)
(hash-table-keys h)

正規表現 (Gauche)

(rxmatch->string #/(\d+)-(\d+)/ "tel 03-1234" 2)  ; => "1234"
(regexp-replace-all #/\s+/ "a  b   c" " ")        ; => "a b c"
(#/^ab/ "abc")                                     ; 正規表現は手続きとして呼べる

入出力

書き方意味
(print a b)続けて表示して改行 (Gauche)
(display x) / (write x)人向け / 読み戻せる形 で表示
(read-line)1 行読む (終わりなら EOF オブジェクト)
(call-with-input-file "f" port->string)ファイルを丸ごと読む
(with-output-to-file "f" (lambda () (print "x")))ファイルに書く
(file->string-list "f")行のリストで読む ((use file.util))

例外と継続

書き方意味
(error "msg" obj)エラーを投げる
(guard (e (#t (condition-message e))) ...)エラーを捕まえる
(call/cc (lambda (k) ...))今の継続を k として受け取る (継続の解説)
(dynamic-wind before thunk after)継続で出入りしても前後の処理を必ず行う