Previous: R7RS small language, Up: ライブラリモジュール - R7RS標準ライブラリ [Contents][Index]
R7RS largeはまだ策定途上ですが、既に決定したライブラリについては 徐々にGaucheに追加してゆきます。
現在、R7RS-largeには2つのエディション (RedとTangerine) が 制定されており、Gaucheでもこれらをサポートしています。
• R7RSリスト: | scheme.list
| |
• R7RSベクタ: | scheme.vector
| |
• R7RSユニフォームベクタ: | scheme.vector.@
| |
• R7RSソート: | scheme.sort
| |
• R7RSセット: | scheme.set
| |
• R7RS文字集合: | scheme.charset
| |
• R7RSハッシュテーブル: | scheme.hash-table
| |
• R7RS変更不可リスト: | scheme.ilist
| |
• R7RSランダムアクセスリスト: | scheme.rlist
| |
• R7RS変更不可な両端キュー: | scheme.ideque
| |
• R7RS変更不可なテキスト: | scheme.text
| |
• R7RSジェネレータ: | scheme.generator
| |
• R7RS遅延シーケンス: | scheme.lseq
| |
• R7RSストリーム: | scheme.stream
| |
• R7RSボックス: | scheme.box
| |
• R7RSリストキュー: | scheme.list-queue
| |
• R7RS Ephemeron: | scheme.ephemeron
| |
• R7RS比較器: | scheme.comparator
| |
• R7RS正規表現: | scheme.regex
| |
• R7RSマップ: | scheme.mapping
| |
• R7RS整数除算: | scheme.division
| |
• R7RSビット演算: | scheme.bitwise
| |
• R7RS fixnum: | scheme.fixnum
| |
• R7RS flonum: | scheme.flonum
| |
• R7RS bytevectors: | scheme.bytevector
| |
• R7RS combinator formatting: | scheme.show
|
Next: R7RSベクタ, Previous: R7RS large, Up: R7RS large [Contents][Index]
scheme.list
- R7RSリストこのモジュールは、豊富なリスト操作手続きのコレクションです (srfi-1と同じです)。
Gaucheはscheme.list
の手続きの多くを組み込みで提供しています。
以下に挙げる手続きはscheme.list
モジュールをロードしなくても使えます。
これらの手続きの説明は、ペアとリストを参照してください。
null-list? cons* last member take drop take-right drop-right take! drop-right! delete delete! delete-duplicates delete-duplicates! assoc alist-copy alist-delete alist-delete! any every filter filter! fold fold-right find find-tail split-at split-at! iota
[R7RS list]
{scheme.list}
(cons ca cd)
と同等です。高階手続きへ渡すのに便利です。
[R7RS list]
{scheme.list}
n個の要素をもつリストを構築し、それぞれの要素を
(init-proc i)
で生成します。
(list-tabulate 4 values) ⇒ (0 1 2 3)
[R7RS list] {scheme.list} 指定した要素をもつ循環リストを構築します。
(circular-list 'z 'q) ⇒ (z q z q z q …)
[R7RS list]
{scheme.list}
(lambda (x) (not (pair? x)))
と同じです。
SRFI-1 では、「真性リストおよびドットリストの両方で、すべての有限リストを 扱う手続き用の終端条件として便利なように用意した」とあります。
[R7RS list] {scheme.list} elt= を用いて、n番目の要素をそれぞれ比較することで、 与えられたリストの同値性を決定します。
list=
を真性リスト以外に適用するとエラーになります。
同値性判定の手続きは eq?
と整合性がなければなりません。すなわち
(eq? x y) ⇒ (elt= x y).
[R7RS list] {scheme.list} リスト(非真性でも可)のn番目の要素を返します。
[R7RS list]
{scheme.list}
(car pair)
および (cdr pair)
の二つの値を返します。
[R7RS list]
{scheme.list}
(map list clist1 clist2 …)
と同等です。
n 本のリストが zip
に渡された場合には、そのなかで一番短いものと
同じ長さのリストを返します。返されたリストは、要素が n 要素のリストで、
そのそれぞれが、引数として渡ってリストの対応する要素になっています。
(zip '(one two three) '(1 2 3) '(odd even odd even odd even odd even)) ⇒ ((one 1 odd) (two 2 even) (three 3 odd)) (zip '(1 2 3)) ⇒ ((1) (2) (3))
引数のリストのうち、少くともひとつは有限のリストでなければなりません。
(zip '(3 1 4 1) (circular-list #f #t)) ⇒ ((3 #f) (1 #t) (4 #f) (1 #t))
[R7RS list]
{scheme.list}
unzip1
はリストのリストを引数としてとります。それぞれの
リストは少くとも一つの要素を含むものでなくてはなりません。結果として
それぞれのリストの最初の要素のリストを返します。
unzip2
はリストのリストを引数としてとります。それぞれのリストは
少くとも二つの要素を含むものでなくてはなりません。結果として二つの値を
返します。最初の要素のリストと二番目の要素のリストです。unzip3
は
3番目までの要素について同様です。以下も同様です。
(unzip2 '((1 one) (2 two) (3 three))) ⇒
(1 2 3) and
(one two three)
[R7RS list]
{scheme.list}
fold
および fold-right
と同様ですが、kons 手続き
は与えられた clist の car
ではなく、cdr
をとります。
(pair-fold cons '() '(a b c d e)) ⇒ ((e) (d e) (c d e) (b c d e) (a b c d e)) (pair-fold-right cons '() '(a b c d e)) ⇒ ((a b c d e) (b c d e) (c d e) (d e) (e))
[R7RS list] {scheme.list} 基本リスト再帰構築子です。 以下のように再帰的に定義されています。
(unfold p f g seed tail-gen) ≡ (if (p seed) (tail-gen seed) (cons (f seed) (unfold p f g (g seed))))
ここでは、p は終了位置の判定、g は現在の「種」から次の「種」 を生成するのに用い、f はそれぞれの「種」をリストの要素に変換する のに用いられます。
(unfold (pa$ = 53) integer->char (pa$ + 1) 48) ⇒ (#\0 #\1 #\2 #\3 #\4)
[R7RS list] {scheme.list} 基本リスト反復構築子です。 以下のように再帰的に定義されています。
(unfold-right p f g seed tail) ≡ (let lp ((seed seed) (lis tail)) (if (p seed) lis (lp (g seed) (cons (f seed) lis))))
(unfold-right (pa$ = 53) integer->char (pa$ + 1) 48) ⇒ (#\4 #\3 #\2 #\1 #\0)
[R7RS list] {scheme.list} 手続き f は clist1 の各要素と clist2 の対応する要素 に適用され、結果はひとつのリストになります。clist1 のセルは 結果のリストを構築するのに再利用されます。
[R7RS list]
{scheme.list}
map
の変形バージョンですが、f の適用順序が、引数として
与えられたリストの要素の左から右への順であることを保証します。
Gauche では map
の実装はこの順になっているので、map
と
同意です。
[R7RS list]
{scheme.list}
for-each
と似ていますが、手続き f はまず clist自体に
適用され、次ににそれらのcdr
に適用され、となります。
(pair-for-each write '(a b c))
⇒ prints (a b c)(b c)(c)
[R7RS list]
{scheme.list}
filter
と remove
を同時に行い、
2つのリストを返します。一つ目は pred により list の要素をフィルタリング
した結果で、二つ目は pred により list の要素を削除した結果です。
(partition odd? '(3 1 4 5 9 2 6)) ⇒ (3 1 5 9) (4 2 6)
partition!
はその場で更新されるバージョンです。結果を生成するために
list を破壊的に更新するかもしれません。
[R7RS list] {scheme.list} clist の最初から、pred を満足する限りの最長部分要素を返します。
[R7RS list] {scheme.list} clist の最初から、pred を満足する限りの最長部分要素を削除し、 残りを返します。
[R7RS list]
{scheme.list}
span
は (values (take-while pred clist) (drop-while pred clist))
と等価です。break
は pred の意味を反転します。
[R7RS list]
{scheme.list}
pred を満足する最も左の要素のインデックスを返します。
predを満たす要素が無い場合は#f
を返します。
[R7RS list]
{scheme.list}
(cons (cons key datum) alist)
を返します。
これは、Gauche の組み込み手続き acons
の別名です。
これらの手続きはリストを集合としてあつかいます。すなわち、どのような 要素があるかは重要ですが、その順序は重要ではありません。
この範疇にあるすべての手続きは、比較手続き elt= を最初の引数として とります。この比較手続きは与えられた二つの集合の要素が等しいかどうかを 判定します。
リストは検索に線形時間を必要とするため、ここに挙げた手続きは 大きな集合を扱うには向いていません。もし対象となる集合が 二桁以上の要素を持つことが分かっているなら、R7RSセットを 参照してください。
集合の要素の組み合せについては 組み合わせ も参照してください。
[R7RS list]
{scheme.list}
list1 のすべての要素が list2 (以降の集合)に含まれている
ときに限り #t
を返します。リストが与えられなかった場合
および一つだけしか与えられなかった場合には、#t
を返します。
[R7RS list]
{scheme.list}
list1 のすべての要素が list2 に含まれており、かつ、
list2 のすべての要素が list1 に含まれていれば、#t
を返します。
(lset= eq? '(b e a) '(a e b) '(e e b a)) ⇒ #t
[R7RS list] {scheme.list} elt … を集合 list にまだなければ、追加します。 (順序はとくに決っていません。)
(lset-adjoin eq? '(a b c) 'a 'e) ⇒ '(e a b c)
[R7RS list] {scheme.list} list1 … の和集合を返します。
[R7RS list] {scheme.list} すべての list に含まれる要素の集合を返します。
[R7RS list] {scheme.list} list1 には含まれていて、list2 には含まれていない要素の集合を 返します。引数が n 個与えられた場合には、差分をとる二項演算が 畳み込まれます。
[R7RS list] {scheme.list} 与えられた集合の排他的論理和を返します。すなわち、list1 および list2 のどちらか一方にのみ属する要素からなる集合を返します。 引数が n 個の場合には、xor の二項演算が畳み込まれます。
[R7RS list] {scheme.list} 与えられた集合の差集合と積集合のふたつの集合を返します。
[R7RS list] {scheme.list} それぞれ対応する手続きのその場で更新するバージョンです。 最初の引数のリストのセルが結果を構築するのに再利用されるかもしれません。
Next: R7RSユニフォームベクタ, Previous: R7RSリスト, Up: R7RS large [Contents][Index]
scheme.vector
- R7RSベクタこのモジュールは豊富なベクタ操作を提供します。
以下の手続きはGaucheでは組み込みになっています。説明はベクタを参照してください。 組み込みでない手続きについてのみ解説します。
make-vector vector vector? vector-ref vector-set! vector-length vector-fill! vector-copy vector-copy! vector-append vector->list list->vector reverse-list->vector vector->string string->vector vector-map vector-map! vector-for-each
このモジュールはsrfi-133
として制定され、
srfi-43
(ベクタライブラリ(旧式))を置き換えるものです。
srfi-43
の手続きのうち、以下のものは一貫性からインタフェースが
変更されていることに注意してください。
vector-map vector-map! vector-for-each vector-fold vector-fold-right vector-count
srfi-43
から削られた機能の一部は、
Gaucheの組み込み手続きでサポートされています
(例えばsrfi-43
のvector-map
は組み込みのvector-map-with-index
と
同じです)。新しいコードでsrfi-43
を使う必要はほとんどないでしょう。
[R7RS vector] {scheme.vector} 長さlengthのベクタを作って返します。要素は左から右に、 fを繰り返し呼ぶことで計算されます。
手続きfは、seedの数よりひとつ多い引数を取り、同数の値を返さなければなりません。 引数の最初に渡されるのはインデックスです。戻り値の最初の値がそのインデックスの要素の初期値に 使われ、残りの戻り値が次のシード値となります。
(vector-unfold (^[i] (* i i)) 5) ⇒ #(0 1 4 9 16) (vector-unfold (^[i x] (values (cons i x) (* x 2))) 8 1) ⇒ #((0 . 1) (1 . 2) (2 . 4) (3 . 8) (4 . 16) (5 . 32) (6 . 64) (7 . 128))
註: このプロトコルは、リストのunfold
コンストラクタや、それに倣った
多くの*-unfold
コンストラクタと異なることに注意してください。
ベクタのような固定長構造体では、終端条件を繰り返しの度に検査するより、
最初から長さが分かっている方が意味があります。
[R7RS vector] {scheme.vector} 長さlengthのベクタを作って返します。要素は右から左に、 fを繰り返し呼ぶことで計算されます。
手続きfは、seedの数よりひとつ多い引数を取り、同数の値を返さなければなりません。 引数の最初に渡されるのはインデックスです。戻り値の最初の値がそのインデックスの要素の初期値に 使われ、残りの戻り値が次のシード値となります。
(vector-unfold-right (^[i] (* i i)) 5) ⇒ #(0 1 4 9 16) (vector-unfold-right (^[i x] (values (cons i x) (* x 2))) 8 1) ⇒ #((0 . 128) (1 . 64) (2 . 32) (3 . 16) (4 . 8) (5 . 4) (6 . 2) (7 . 1))
[R7RS vector] {scheme.vector} ベクタvecを要素を逆順にしてコピーします。 省略可能引数startとendは元のベクタから取り出す範囲を限定します。
(vector-reverse-copy '#(a b c d e) 1 4) ⇒ #(d c b)
[R7RS vector]
{scheme.vector}
(apply vector-append list-of-vectors)
と同じです。
[R7RS vector] {scheme.vector} 引数の数は3の倍数でなければなりません。 引数リストは次の形式をとります。ここで、vecNはベクタ、 startNとendNは非負の正確な整数です。
vec1 start1 end1 vec2 start2 end2 …
この手続きは、それぞれの3つ組で指定される部分ベクタをつなぎ合わせたベクタを 作って返します。つまり、以下のコードと同様の動作をしますが、 各部分ベクタのコピーは避けられます。
(vector-append (vector-copy vec1 start1 end1) (vector-copy vec2 start2 end2) …)
例を示します:
(vector-append-subvectors '#(a b c d e) 0 3 '#(f g h i j) 2 5) ⇒ #(a b c h i j)
[R7RS vector]
{scheme.vector}
ベクターvecの長さが0なら#t
を、1以上なら#f
を返します。
vecがベクタでなければエラーが投げられます。
[R7RS vector]
{scheme.vector}
vec …を要素ごとに比較してゆきます。要素間の比較にはelt=が使われます。
全てのベクタ引数が同じ長さで、対応する要素がelt=によって等しかった場合に、#t
が返されます。
elt=は常に二つの引数を取り、
等しければ#t
を、そうでなければ#f
を返します。
ゼロ個か1個のベクタが渡された場合は、elt=は全く呼ばれず、
vector=
は常に#t
を返します。
[R7RS vector]
{scheme.vector}
konsは与えられたベクタ引数vec1 vec2 …
の数よりひとつ多い引数を取る手続きです。
各ベクタのi番目の要素e_1i e_2i …)に
対して、konsが
(kons seed e_1i e_2i …)
のように順次呼ばれます。ベクタの長さが異なっていた場合、最も短いベクタが尽きたところで
繰り返しは停止します。
seedの初期値はknilで、konsの戻り値が次のseedとして
使われます。最後のkonsの戻り値がvector-fold
の戻り値となります。
繰り返しはベクタの左から右へと進みます。
seed引数が要素の前に来ることに注意してください。これは
fold
(コレクションに対するマッピング参照)とは逆です。
歴史的に生じた不幸なすれちがいです。
vector-fold-left
であれば一貫性があったでしょう。
(vector-fold (^[a b] (cons b a)) '() '#(a b c d)) ⇒ (d c b a)
[R7RS vector]
{scheme.vector}
vector-fold
と似ていますが、vec1 vec2 …の各要素は
右から左へと読まれます。
ベクタの長さが異なる場合は、最も短いベクタの要素数までを見て、残りは無視します。 下の2番目の例を見てください。
fold-right
(シーケンス上のマップ参照)とは異なり、
konsはシード値を最初の引数として受け取ります。
(vector-fold-right (^[a b] (cons b a)) '() '#(a b c d)) ⇒ (a b c d) (vector-fold-right (^[s x y] (cons (list x y) s)) '() '#(a b c) '#(1 2 3 4)) ⇒ ((a 1) (b 2) (c 3))
[R7RS vector] {scheme.vector} Applies pred on each elements in argument vectors (if N vectors are given, pred takes N arguments, the first being i-th element of vec1, the second being i-th element of vec2, etc.) Then returns the number of times pred returned true value. The order pred applied to each element is unspecified.
(vector-count odd? '#(0 1 2 3 4) ⇒ 2 (vector-count < '#(7 3 9 1 5) '#(6 8 2 3 8 8)) ⇒ 3
[R7RS vector] {scheme.vector} Returns a fresh vector with the same size of vec, with the elements calculated as follows:
The first element of result vector is a result of procedure f called with seed and the first element of vec.
The i-th element of result vector is a result of procedure f called with i-1-th element of result vector and i-th element of vec.
(vector-cumulate string-append "z" '#("a" "b" "c")) ⇒ #("za" "zab" "zabc")
[R7RS vector]
{scheme.vector}
Returns the index of the first or the last elements in vec1
vec2 … that satisfy pred, respectively.
Returns #f
if no elements satisfy pred.
In vector-index
, comparison ends at the end of the shortest vector.
For vector-index-right
, all the vectors must have the same length.
[R7RS vector]
{scheme.vector}
Like vector-index
and vector-index-right
, except that
the result of pred is negated. That is, returns the index
of the first or the last elements that don’t satisfy pred.
[R7RS+]
{scheme.vector}
ソートされたベクタvec中から値valueを探し、
見つかればそのインデックスを、見つからなければ#f
を返します。
vecの要素とvalueの比較は手続きcmpで行われます。 cmpは二つの引数を取り、最初の引数の方が小さければ負の整数を、 等しければ0を、最初の引数の方が大きければ正の整数を買えす手続きです。
vecの要素はcmpによる比較に従い小さい方から順にソート済みで なければなりません。この手続きはその仮定のもとに、二分探索を行います。
省略可能なstartとend引数は、Gauche独自の拡張です。指定された場合、 start番目の要素(含む)からend番目の要素(含まない)までが探索の対象となります。
[R7RS vector] {scheme.vector} vec1 vec2 …の各ベクタの先頭から順に、 ひとつづつ対応する要素を取って、それにpredを適用してゆきます。 predが真の値を返したら、直ちにその値を返します。
predを満たす要素が見つからなければ、#f
が返されます。
ベクタの長さは異なっていても構いません。最も短いベクタの要素を使い切ったところで 探索は打ちきられます。
[R7RS vector]
{scheme.vector}
vec1 vec2 …の各ベクタの先頭から順に、
ひとつづつ対応する要素を取って、それにpredを適用します。
全ての要素がpredを満たした場合(ベクタの長さが異なる場合は、
最も短いベクタを使い切った時点)で、最後のpredの結果を返します。
途中でpredが#f
を返した場合は、その先は見ずに直ちに#f
を返します。
(vector-every < '#(1 2 3 4 5) '#(2 3 4 4 5) ⇒ #f (vector-every (^[x y] (and (real? x) (real? y) (- x y))) '#(1 2 3) '#(2 4 6)) ⇒ -3
[R7RS vector] {scheme.vector} vecと同じ大きさのベクタを新たにアロケートし、 まずvec中のpredを満たす要素を順に詰めてゆき、 その後をpredを満たさない要素で埋めます。
新たに作られたベクタと、その中でpredを満たさない最初の要素を指す インデックスの二つの値を返します。
(vector-partition odd? '#(1 2 3 4 5 6 7 8))
⇒ #(1 3 5 7 2 4 6 8) and 4
[R7RS vector] {scheme.vector} vecのi番目の要素とj番目の要素を交換します。 戻り値はunspecifiedです。
(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-swap! v 0 2)) ⇒ #(c b a d e)
[R7RS vector]
{scheme.vector}
vec
の要素を逆順に並べ替えます。戻り値は未定義です。
省略可能引数startとendは影響が及ぶ範囲を限定します。
(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-reverse! v 0 4)) ⇒ #(d c b a e)
[R7RS vector]
{scheme.vector}
vector-copy!
と似ていますが、指定範囲を逆順にコピーします。
(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-reverse-copy! v 2 '#(1 2))) ⇒ #(a b 2 1 e)
targetとsourceに同じベクタを指定しても構いません。 コピー範囲が重なっていても問題なく動作します。
(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-reverse-copy! v 1 v 1)) ⇒ #(a e d c b)
[R7RS vector] {scheme.vector} rvecの、startからendの直前までの範囲をfが算出する 新たな要素で埋めます。
fは、seeds …の数よりひとつ多い引数を取ります。 最初の引数は現在のインデックスで、それにシード値が続きます。 fは引数と同数の値を返します。最初の戻り値がrvecを埋めるのに使われ、 残りの戻り値は新たなシード値として次のfの呼び出しに使われます。
結果のベクタは、vector-unfold!
では左から右へ、
vector-unfold-right!
では右から左へと埋められます。
戻り値はunspecifiedです。
(let1 rvec (vector 'a 'b 'c 'd 'e 'f) (vector-unfold! (^[i] (+ i 1)) rvec 1 4) rvec) ⇒ #(a 2 3 4 e f) (let1 rvec (vector 'a 'b 'c 'd 'e 'f) (vector-unfold-right! (^[i] (+ i 1)) rvec 1 4) rvec) ⇒ #(a 2 3 4 e f) (let1 rvec (vector 'a 'b 'c 'd 'e 'f) (vector-unfold! (^[i x] (values x (* x 2))) rvec 1 5 10) rvec) ⇒ #(a 10 20 40 80 f) (let1 rvec (vector 'a 'b 'c 'd 'e 'f) (vector-unfold! (^[i x] (values x (* x 2))) rvec 1 5 10) rvec) ⇒ #(a 80 40 20 10 f)
[R7RS vector]
{scheme.vector}
(reverse (vector->list vec start end))
と同じですが、より効率的です。
Next: R7RSソート, Previous: R7RSベクタ, Up: R7RS large [Contents][Index]
scheme.vector.@
- R7RSユニフォームベクタ@
は次のいずれかに置き換えて読んでください:
u8
, s8
, u16
, s16
,
u32
, s32
, u64
, s64
, f32
,
f64
, c64
, c128
。
(Gaucheのgauche.uvector
モジュールはさらにf16
とc32
を
提供します)。
これらのモジュールは限られた範囲の数値だけを格納するベクタを提供します。 Gaucheでは、数値は連続したメモリ領域に詰められて格納されます。
また、scheme.vector.base
モジュールは全ての要素型についての
基本手続き、つまり次の手続きの@
を全ての基本型で置換した手続きをまとめて
エクスポートしています:
make-@vector
、@vector
、@vector?
@vector-length
、@vector-ref
、@vector-set!
、
@vector->list
、list->@vector
、@?
gauche.uvector
モジュールはこれらのモジュールのスーパーセットで、
このモジュールの全ての手続きはそちらで説明されています。
ユニフォームベクタを参照してください。
Next: R7RSセット, Previous: R7RSユニフォームベクタ, Up: R7RS large [Contents][Index]
scheme.sort
- R7RSソートソートおよびソート済みのリストやベクタに関する手続きを提供します。
このモジュールはsrfi-132
と同じです。
Gaucheは組み込みでソートとマージの手続きを用意していますが (ソートとマージ参照)、 このモジュールはAPIがちょっと異なります。特に、順序を決める述語が ソートすべきシーケンスより先に来ます。また、ベクタを扱う手続きは一様に start/end引数をサポートします。
このモジュールはまた、(部分的に)ソートされた列に対する便利な手続きをいくつか 定義しています。
[R7RS sort]
{scheme.sort}
リストlisの要素を比較述語elt<を使ってソートします。
elt<はlisからの二要素を引数に取り、最初の引数が厳密に
二番目の引数より小さい場合に限り#t
を返します。
ソートされたリストが返されます。!
つきの手続きは線形更新版で、
lisを破壊的に再利用することが許されています (再利用しないかもしれません。
したがって呼び出し側は常に戻り値を使う必要があります)。 “stable” がついている
ものは安定ソートを行います。
これらは基本的にはGaucheの組み込み手続き
sort
、sort!
、stable-sort
、stable-slot!
と
同じです。ただ、Gaucheのは引数の順序と、任意のシーケンスを取れるところが異なります
(ソートとマージ参照)。
[R7RS sort] {scheme.sort} リストlistが、順序述語elt<での比較において既にソート済みであれば 真を、そうでなければ偽を返します。
ソートとマージのsorted?
も参照。
[R7RS sort] {scheme.sort} ソート済みの二つのリストlis1とlis2を合わせて、 ソート済みのひとつのリストにして返します。要素は順序述語elt<で比較されます。
list-merge!
は、結果のリストを得るためにlis1とlis2の
メモリを破壊的に再利用します。
ソートとマージのmerge
およびmerge!
も参照。
[R7RS sort] {scheme.sort} Sort elements in a vector vec according to the ordering predicate elt<, which takes two elements from vec and returns true iff the first argument is strictly less than the second argument. Returns a fresh sorted vector. The “stable” variation guarantees stable sort.
When the optional start and/or end arguments are given,
only the portion from start (inclusive) and
end (exclusive) of vec are looked at.
The result vector’s length is end - start)
.
When end is omitted, the length of vec is assumed.
See also sort
and stable-sort
in ソートとマージ.
[R7RS sort] {scheme.sort} Sort elements “in-place” in a vector vec according to the ordering predicate elt<, which takes two elements from vec and returns true iff the first argument is strictly less than the second argument. Upon successful return, vec’s elements are sorted. Returns unspecified value; the caller must rely on the side effect.
When the optional start and/or end arguments are given, only the portion from start (inclusive) and end (exclusive) of vec are sorted; other elements will remain intact. When end is omitted, the length of vec is assumed.
See also sort!
and stable-sort!
in ソートとマージ.
[R7RS sort] {scheme.sort} Returns true iff vec between start (inclusive) and end (exclusive) is sorted according to the ordering predicate elt<. If start and/or end is/are omitted, 0 and the length of vec are assumed, respectively.
See also sorted?
in ソートとマージ.
[R7RS sort] {scheme.sort} Merge two sorted vectors vec1 and vec2 into one vector, according to the ordering predicate elt<.
The optional argument start1 and end1 restricts vec1’s portion to be looked at, and start2 and end2 restricts vec2’s portion to be looked at.
The functional version vector-merge allocates a fresh vector to hold the result, and returns it.
The side-effecting version vector-merge! uses rvec. to hold the result. The procedure doesn’t return a meaningful value. The optional rstart argument specifies the index of rvec from which the result is filled; the default is 0.
[R7RS sort] {scheme.sort} From the given list lis or vector vec, these procedures delete adjacent duplicate elements. Equivalence is checked by elt= procedure.
(list-delete-neighbor-dups eq? '(m i s s i s s i p p i)) ⇒ (m i s i s i p i)
The non-destructive versions list-delete-neighbor-dups
and
vector-delete-neighbor-dups
returns a freshly allocated
list and vector, respectively.
The destructive list-delete-neighbor-dups!
works in-place,
reusing pairs of lis. No allocation will be done.
The destructive vector-delete-neighbor-dups!
has a bit different
interface. It updates vec in-place, but since we can’t change
the length of the vector, it gathers the result from the beginning of
the vec, then returns the next index newend
of vec—that is,
after calling this procedure, [start, newend)
holds the result.
The elements between [newend, end)
will remain intact.
The optional start and end arguments limits the region of vec to be looked at.
(vector-delete-neighbor-dups eq? '#(a a a b b c c d d e e f f) 3 10) ⇒ #(b c d e) (let1 v '#(a a a b b c c d d e e f f) (cons (vector-delete-neighbor-dups! eq? v 3 10) v)) ⇒ (7 . #(a a a b c d e d d e e f f))
Note: The gauche.sequence
module provides neighbor duplicate
deletion on generic sequences. Those procedures are implemented by
the generic versions as shown below. See その他のシーケンス上の操作,
for the details.
list-delete-neighbor-dups
delete-neighbor-dups
list-delete-neighbor-dups!
delete-neighbor-dups-squeeze!
vector-delete-neighbor-dups
delete-neighbor-dups
vector-delete-neighbor-dups!
delete-neighbor-dups!
[R7RS sort]
{scheme.sort}
Select k-th smallest element in vec according to the
ordering predicate elt<. K is zero based, i.e. 0 means
the smallest. The optional start and end arguments
limits the range of vec to be looked at, and defaulted to
0 and the length of vec, respectively.
K must satisfy start <= k < end
.
This procedure runs in O(n) time, and requires no extra stroage. This procedure may partially modify vec.
[R7RS sort]
{scheme.sort}
Find k-th smallerst element in vec (pivot) between
between start and end, according to the ordering
predicate elt<, then rearrange elements
between start and end so that
elements smaller than the pivot comes between start
and
start + k
, and the rest of the elements come
afterwards. When omitted, start is 0 and end is
the length of the vec.
This can be used as a building block for in-place divide-and-conquer algorithms. Runs in O(n) time.
[R7RS sort]
{scheme.sort}
Find median value of elements in vec, when ordered by
the ordering predicate elt<. Non-destructive version
vector-find-median
runs in O(n) time. The destructive
version vector-find-median!
is specified to leave
vec sorted, so it runs in O(n log n).
(vector-find-median < #() 0) ⇒ 0 (vector-find-median < #(78 61 19 38 51) 0) ⇒ 51 (vector-find-median < #(78 61 19 38 51 52) 0) ⇒ 103/2
Next: R7RS文字集合, Previous: R7RSソート, Up: R7RS large [Contents][Index]
scheme.set
- R7RSセットセットとバッグは、Scheme値の順序づけのないコレクションです。 セットは重複を考慮しません。既にセット中にある要素をさらに追加しても、 その要素は依然として一つだけセット中にあると認識されます。 一方、バッグは重複を数えます。既にバッグ中にひとつだけある要素と同じものを 追加すると、バッグ中のその要素は二つと数えられます。
要素が「同じ」であるかどうかの判定のため、セットとバッグは構築時に 比較器を取ります。比較器は順序手続きを持っていなくても構いません (要素の順序づけは必要ありません)が、ハッシュ手続きは持っている必要があります。 比較器について詳しくは基本的な比較器を参照してください。
このモジュールは当初srfi-113
として定義され、のちにR7RS largeに
採用されました。
Gauche独自拡張として、セットやバッグはコレクションプロトコルを実装しており、 汎用のコレクション操作が適用できます (コレクションフレームワーク参照)。
(coerce-to <list> (set eq-comparator 'a 'b 'a 'b)) ⇒ (a b) ; order may differ (coerce-to <list> (bag eq-comparator 'a 'b 'a 'b)) ⇒ (a a b b) ; order may differ
[R7RS set] {scheme.set} Creates a new set and bag from given elements elt …. Given comparator will be used to compare equality of elements.
(set->list (set eq-comparator 'a 'b 'a 'b)) ⇒ (a b) (bag->list (bag eq-comparator 'a 'b 'a 'b)) ⇒ (a a b b)
[R7RS set] {scheme.set} Procedurally creates a set or a bag. The first three arguments, stop?, mapper and successor, are all procedures that takes one argument, the current seed value. It may be easier to know their types:
seed :: Seed stop? :: Seed -> Boolean mapper :: Seed -> ElementType successor :: Seed -> Seed
The stop?
procedure takes the current seed value and
returns a boolean value - if it is true, iteration stops.
The mapper
procedure takes the current seed value
and returns an item, which is to be included in the resulting set or bag.
The successor
procedure takes the current seed value
and returns the next seed value.
And the seed
argument gives the initial seed value.
(set->list (set-unfold (^s (= s 75)) integer->char (^s (+ s 1)) 65 eqv-comparator)) ⇒ (#\D #\H #\A #\E #\I #\J #\B #\F #\C #\G)
[R7RS set] {scheme.set} Check if obj is in the set or the bag.
[R7RS set]
{scheme.set}
Returns #t
iff the given set or bag is empty.
[R7RS set]
{scheme.set}
Returns #t
iff the given arguments (sets or bags) don’t have
common items. Both arguments must have the same comparator—otherwise
an error is signaled.
[R7RS set] {scheme.set} Returns an element in the given set or bag which is equal to obj in terms of the set’s or the bag’s comparator. If no such element is found, default will be returned.
Note that the returned object doesn’t need to be “the same” as obj in a usual sense. See the following example:
(let s (set string-ci-comparator "abc" def") (set-member s "ABC" #f)) ⇒ "abc"
[R7RS set] {scheme.set} Returns the comparator used to compare the elements for the set or the bag.
[R7RS set] {scheme.set} Returns a newly created set or bag that contains all the elements in the original set/bag, plus given elements. The new set/bag’s comparator is the same as the original set/bag’s one.
[R7RS set] {scheme.set} Returns a newly created set/bag with the same comparator with the original set/bag, and the same elements, except that the elements equal to elt (in terms of set/bag’s comparator) is replaced by elt. If the original set/bag doesn’t contain an element equal to elt, the original one is returned.
(let ((s (set string-ci-comparator "ABC" "def"))) (set->list (set-replace s "abc"))) ⇒ ("abc" "def")
[R7RS set] {scheme.set} Returns a newly created set or bag that has the same comparator and the same elements in the original set/bag, except that the item which is equal to elt.
[R7RS set] {scheme.set} Returns a newly created set or bag with the same comparator of the original set/bag, with the elements of the original set/bag except the ones listed in elt-list.
[R7RS set]
{scheme.set}
These are the linear update versions of their counterparts.
It works just like the ones without !
, except that
the original set/bag may be reused to produce the result,
instead of new one being allocated.
Note that it’s not guaranteed that the original set/bag is modified, so you should use the return value of them, instead of relying on the side effects.
[R7RS set] {scheme.set} Lookup-and-modify procedures. The failure and success arguments are procedures.
First, they search elt in the given set/bag. If an item that matches elt is found, the success procedure is called with three arguments, as follows:
(success item update remove)
The update argument is a procedure that takes two arguments,
as (update new-item retval)
. It replaces the matching item in the
set/bag with new-item, and returns retval.
The remove argument is a procedure that takes one argument,
as (remove retval)
. It removes the matching item in
the set/bag, and returns retval.
If an item that matches elt is not found, the failure procedure is called with two arguments, as follows:
(failure insert ignore)
The insert argument is a procedure that takes one argument,
as (insert retval)
. It inserts elt into the set/bag, and
returns retval.
The ignore argument is a procedure that takes one argument,
as (ignore retval)
. It just returns retval.
The return values of set-search!
and bag-search!
is
the modified set/bag (which may or may not be eq?
to the passed one),
and the value returned by success or failure procedures.
Note that retval isn’t used in this process; it is just to provide
one of the return values of set-search!
/bag-search!
, for
the procedures passed to success or failure are expected
to be tail-called.
If there are more than one item that matches elt in a bag,
bag-search!
only invokes success for the first item
it finds. You can recurse into bag-search!
in the failure
procedure to visit all matching items. It is guaranteed that success
and failure procedures are tail-called.
[R7RS set] {scheme.set} Returns the number of items in the set/bag.
[R7RS set] {scheme.set} Apply pred on each item in the set/bag, and returns the first item on which pred returns true. Since sets and bags are unordered, if there are more than one items that satisfy pred, you won’t know which one will be returned.
If there’re no items that satisfy pred, a thunk failure
is called and its result is returned.
[R7RS set] {scheme.set} Returns the number of items that satisfy pred in the set/bag.
[R7RS set] {scheme.set} Returns true iff any item in the set/bag satisfy pred.
[R7RS set] {scheme.set} Returns true iff every item in the set/bag satisfy pred.
[R7RS set] {scheme.set} Create and return a new set/bag with the comparator comparator, whose items are calculated by applying proc to each element in the original set/bag.
[R7RS set] {scheme.set} Apply proc to each element in the set/bag. The result of proc is ignored. Return value is undefined.
[R7RS set]
{scheme.set}
For each item in the set/bag, call proc with two arguments, an item
and a seed value. What proc returns becomes the next seed value.
The seed argument gives the initial seed value, and the last return
value of proc will be the result of set-fold
/bag-fold
.
(bag-fold + 0 (bag eqv-comparator 1 1 2 2 3 3 4 4)) ⇒ 20
[R7RS set] {scheme.set} Returns a newly created set/bag with the same comparator of the original set/bag, and its content consists of items from the original set/bag that satisfy pred.
(set->list (set-filter odd? (set eqv-comparator 1 2 3 4 5))) ⇒ (1 3 5)
[R7RS set] {scheme.set} Returns a newly created set/bag with the same comparator of the original set/bag, and its content consists of items from the original set/bag that does not satisfy pred.
(set->list (set-remove odd? (set eqv-comparator 1 2 3 4 5))) ⇒ (2 4)
[R7RS set] {scheme.set} Returns two sets or bags, both have the same comparator of the original set or bag. The first one consists of the items from the original set/bag that satisfy pred, and the second one consists of the items that don’t.
(receive (in out) (set-remove odd? (set eqv-comparator 1 2 3 4 5))
(values (set->list in)
(set->list out)))
⇒ (1 3 5) and (2 4)
[R7RS set]
{scheme.set}
Linear update versions of their counterparts (the procedures without !
).
They work like their respective counterpart, but they are allowed (but not
required) to reuse the original set/bag to produce the result(s).
Note that it is not guaranteed that the original set/bag is modified, so you have to use the return value(s) instead of relying on the side effects.
[R7RS set] {scheme.set} Returns a copy of the set/bag.
[R7RS set] {scheme.set} Returns a list of all items in the set/bag. Since sets and bags are unordered, there’s no guarantee on the order of items.
[R7RS set] {scheme.set} Creates a set or a bag with the given comparator, and the list of element. Functionally equivalent to the followings:
(apply set comparator elt-list) (apply bag comparator elt-list)
[R7RS set] {scheme.set} Add items in elt-list to the existing set/bag, and returns the updated set/bag. The original set/bag is also modified. Functionally equivalent to the followings:
(apply set-adjoin! set elt-list) (apply bag-adjoin! bag elt-list)
[R7RS set] {scheme.set} Conversions between a bag and a set. Returns a newly created bag or set, respectively.
If bag has duplicated items, bag->set
coerces them to one
item.
[R7RS set] {scheme.set} Adds all items in set to bag, and returns bag. Both bag and set must have the same comparator.
[R7RS set]
{scheme.set}
Returns a list of (item . count)
, where item is
an item in bag, and count is the number of that item
in the bag.
[R7RS set]
{scheme.set}
Creates a new bag with comparator, and fills it according to
alist, which must be a list of (item . count)
.
If there’s duplicate items in alist, only fist one counts.
[R7RS set] {scheme.set} Returns true iff all sets/bags have exactly same items.
The comparators of the argument sets/bags are not checked, but assumed to be the same, in terms of the equality of items.
[R7RS set] {scheme.set} Returns true iff each preceding set/bag is a proper subset of, a proper superset of, a subset of, or a superset of the following set/bags, respectively.
Again, the comparators of the argument sets/bags are not checked, but assumed to be the same, in terms of the equality of items.
[R7RS set] {scheme.set} Returns a newly allocated set or bag which is a union of all the sets/bags.
[R7RS set] {scheme.set} Returns a newly allocated set or bag which is an intersection of all the sets/bags.
[R7RS set] {scheme.set} Returns a newly created set or bag that contains items in set1/bag1 except those are also in set2/bag2 ….
(sort (set->list (set-difference (set eqv-comparator 1 2 3 4 5 6 7) (set eqv-comparator 3 5 7 9 11 13) (set eqv-comparator 4 8 16 32)))) ⇒ (1 2 6)
[R7RS set] {scheme.set} Returns a newly created set or bag that consists of items that are either in set1/bag1 or set2/bag2, but not in both.
(sort (set->list (set-xor (set eqv-comparator 2 3 5 7 11 13 17) (set eqv-comparator 3 5 7 9 11 13 15)))) ⇒ (2 9 15 17)
[R7RS set]
{scheme.set}
Linear update versions of their corresponding procedures.
Those procedures works like their !
-less counterparts,
except that they are allowed to, but not required to, reuse
set1/bag1 to produce the result.
The caller should always use the returned set/bag instead of relying on the side effects.
[R7RS set]
{scheme.set}
Returns a bag that gathers all the items in given bags, counting
duplicates. The functional version bag-sum
always
creates new bag to return.
The linear update version bag-sum!
is allowed to,
but not required to, modify bag1 to produce the result.
(sort (bag->list (bag-sum (bag eqv-comparator 1 1 2 4 5 5 6) (bag eqv-comparator 3 3 5 9)))) ⇒ (1 1 2 3 3 4 5 5 5 6 9)
Note the difference from bag-union
:
(sort (bag->list (bag-union (bag eqv-comparator 1 1 2 4 5 5 6) (bag eqv-comparator 3 3 5 9)))) ⇒ (1 1 2 3 3 4 5 5 6 9)
[R7RS set]
{scheme.set}
Returns a bag that contains every item as n-times
many as the original bag. A fresh bag is created and returned
by bag-product
, while bag-product!
may reuse
bag to produce the result.
(sort (bag->list (bag-product 2 (bag eq-comparator 'a 'b 'r 'a)))) ⇒ (a a a a b b r r)
[R7RS set] {scheme.set} Returns the number of unique elements in bag.
(bag-unique-size (bag eqv-comparator 1 1 2 2 3 3 4)) ⇒ 4
[R7RS set] {scheme.set} Returns the number of specified element elt in bag.
(bag-element-count (bag eqv-comparator 1 1 2 2 2 3 3) 2) ⇒ 3
[R7RS set]
{scheme.set}
For each unique item in bag, calls proc
with two arguments:
The item, and the count of the item in the bag.
[R7RS set]
{scheme.set}
For each unique item in bag, calls proc with three arguments:
The item, the count of the item, and the previous seed value.
The seed argument provides the initial seed value; the result
of proc is used for the next seed value, and the last
result of proc
is returned from bag-fold-unique
.
(sort (bag-fold-unique acons '() (bag equal-comparator "a" "a" "b" "b" "b" "c" "d")) string<? car) ⇒ (("a" . 2) ("b" . 3) ("c" . 1) ("d" . 1))
[R7RS set]
{scheme.set}
Linear update bag to increase or decrease the count of elt in it
by count, which must be an exact integer. Note that the
element count won’t get below zero; if a bag has two a
’s, and
you call (bag-decrement! bag 'a 100)
, you get a bag
with zero a
’s.
[R7RS comparator] {scheme.set} Comparators to be used to compare sets or bags. They don’t provide comparison procedure, for you cannot define a total order among sets or bags. They do provide hash functions.
Next: R7RSハッシュテーブル, Previous: R7RSセット, Up: R7RS large [Contents][Index]
scheme.charset
- R7RS文字集合文字集合ライブラリを実装します。元はSRFI-14で定義されたものです。 以下の文字集合手続きと定義済み文字集合はGauche組み込みになっているので、 文字集合を参照してください。
char-set char-set? char-set-contains? char-set-copy char-set-complement char-set-complement! char-set:lower-case char-set:upper-case char-set:title-case char-set:letter char-set:digit char-set:letter+digit char-set:graphic char-set:printing char-set:whitespace char-set:iso-control char-set:punctuation char-set:symbol char-set:hex-digit char-set:blank char-set:ascii char-set:empty char-set:full
Gaucheでは<char-set>
クラスは<collection>
を継承し、
コレクションプロトコルを実装しているので、gauche.collection
モジュールで
提供される操作も使えます (コレクションフレームワーク参照)。
• 文字集合の構築子: | ||
• 文字集合の比較: | ||
• 文字集合のイテレーション: | ||
• 文字集合への問い合わせ: | ||
• 文字集合に適用できる代数的関数: |
[R7RS comparator]
{scheme.charset}
与えられた文字のリストchar-listから文字集合を構築して返します。
文字集合base-csが与えられていた場合、返される文字集合は
その文字集合にchar-list中の文字を追加したものとなります。
list->char-set!
は、結果を格納するためにbase-csを
変更するかもしれません。
[R7RS charset]
{scheme.charset}
追加する文字をリストでなく文字列sで与えることを除けば
list->char-set
、list->char-set!
と同じです。
[R7RS charset]
{scheme.charset}
char-set内の文字のうち、(pred c)
が真の値を
返すようなcからなる文字集合を作成して返します。
文字集合base-csが与えられた場合は、その内容が結果に追加されます。
char-set-filter!
は、結果を格納するためにbase-csを変更するかも
しれません。
[R7RS charset] {scheme.charset} lower以上、upper未満のUnicodeコードポイントを持つ文字を服務 文字集合を作って返します。
指定された範囲内に文字を割り当てられていないコードポイントがあった場合、 それは単に無視されます。
指定された範囲内にある有効なコードポイントをGaucheがサポートしていない場合、 error?が偽なら(デフォルト)それは単に無視され、真の値ならエラーが投げられます。 デフォルトではGaucheはutf-8内部エンコーディングでコンパイルされていて、 有効な全てのUnicodeコードポイントが扱えます。しかし、Gaucheをそれ以外の内部エンコーディング でコンパイルした場合は、サポートされないコードポイントがあります。
文字集合base-csが与えられた場合、その内容が結果の文字集合に追加されます。
ucs-range->char-set!
は、結果を格納するためにbase-csを
再利用するかもしれません。
{scheme.charset}
これらはGauche特有で、scheme.charset
の仕様には含まれていません。
Gaucheがutf-8内部エンコーディングでコンパイルされていた場合(デフォルト)、これらは
それぞれucs-range->charset
およびucs->range-char-set!
と同じです。
Gaucheがそれ以外の内部エンコーディングでコンパイルされていた場合は、
範囲指定がそのエンコーディングでの文字コードで解釈されません。
error?およびbase-cs引数の意味はucs-char->char-set
と同じです。
[R7RS charset] {scheme.charset} 様々な種類のオブジェクトを文字集合に変換する関数です。xに 文字のコレクション、文字集合、あるいは文字を渡すことができます。 引数が文字集合の場合はそれがそのまま返されます。 引数が文字の場合は、その文字のみを含む文字集合が返されます。
註: R7RS (scheme charset)
の->char-set
は文字列、文字集合、文字のいずれかのみを
引数に取ります。Gaucheでは文字の任意のコレクションを取るように拡張しました。
Next: 文字集合のイテレーション, Previous: 文字集合の構築子, Up: R7RS文字集合 [Contents][Index]
[R7RS charset]
{scheme.charset}
与えられた文字集合が全て同じ要素を持っている場合に限り#t
を返します。
(char-set=) ⇒ #t (char-set= (char-set)) ⇒ #t (char-set= (string->char-set "cba") (list->char-set #\a #\b #\c)) ⇒ #t
[R7RS charset]
{scheme.charset}
各char-set引数がそれ以降のchar-setの部分集合になっている場合に限り#t
を返します。
引数が無い場合は#t
を返します。
[R7RS charset]
{scheme.charset}
char-setのハッシュ値を非負の正確な整数で返します。boundが与えられた場合、
それは正の正確な整数でなければならず、戻り値は0から(- bound 1)
まで(両端含む)の
値を取ります。
Next: 文字集合への問い合わせ, Previous: 文字集合の比較, Up: R7RS文字集合 [Contents][Index]
[R7RS charset]
{scheme.charset}
char-set中の最初の文字を指すオブジェクトを返します。
(ここでの「最初」は文字集合を順にたどってゆく際の最初ということです。
文字集合をたどる順は実装依存で、特定の順序を当てにしないようにしてください。)
返されたオブジェクトは、そのままchar-set-ref
、
char-set-cursor-next
、end-of-char-set?
のcursor引数に
渡す以外に使ってはいけません。
[R7RS charset] {scheme.charset} char-set中の、cursorに指された文字を返します。
cursorはchar-set-cursor
かchar-set-cursor-next
によって
char-setから作られたオブジェクトでなければなりません。
cursorがchar-setから作られたものでない場合の動作は未定義です。
[R7RS charset] {scheme.charset} char-set中の、cursorが指している文字の次の文字を指す新たなカーソルを返します。
cursorはchar-set-cursor
かchar-set-cursor-next
によって
char-setから作られたオブジェクトでなければなりません。
cursorがchar-setから作られたものでない場合の動作は未定義です。
[R7RS charset]
{scheme.charset}
cursorが文字集合の終端を指していたら#t
を、そうでなければ#f
を返します。
[R7RS charset]
{scheme.charset}
char-set中の全ての文字について、手続きkonsを、文字とシード値を引数にして
呼び出します。knilが最初のシード値を与え、konsの戻り値が次のシード値となります。
最後のシード値がchar-set-fold
の戻り値となります。
文字を取り出す順番は規定されていません。
[R7RS charset] {scheme.charset} funをシード値に適用して得られる文字から文字集合を作って返します。 繰り返しの度に、まずpredが現在のシード値に対して呼ばれ、それが真の値を返したら 繰り返しは終了で、それまで作られた文字を集めた集合が戻り値となります。 そうでなければfunがシード値を引数として呼ばれ、文字を返します。 次にgenがシード値を引数として呼ばれ、次のシード値を作ります。
base-char-setが与えられた場合は、それと生成された文字との和集合が返されます。
線形更新バージョンであるchar-set-unfold!
は、base-char-setを破壊的に
再利用するかもしれません。
[R7RS charset] {scheme.charset} procをchar-set中のそれぞれの文字に適用します。 procの戻り値は捨てられます。この手続きの戻り値は規定されません。
[R7RS charset] {scheme.charset} procをchar-set中のそれぞれの文字に適用します。 procの戻り値は文字でなければなりません。procの戻り値の文字を集めた 新たな文字集合を返します。
Next: 文字集合に適用できる代数的関数, Previous: 文字集合のイテレーション, Up: R7RS文字集合 [Contents][Index]
[R7RS charset] {scheme.charset} これらの手続きは、predをchar-setに含まれる各文字に適用します。
char-set-every
はpredが#f
を返したら直ちに#f
を返します。
predが最後まで#f
を返さなかった場合は、最後のpredの返り値が
char-set-every
の返り値となります。
char-set-any
はpredが真の値を返した場合に直ちにその値を返します。
predが最後まで#f
を返した場合には#f
を返します。
char-set-count
はpredが真の値を返した回数を返します。
char-setは非常に大きくなる可能性があり (例えばごく小さな文字集合の補集合)、 その場合、これらの手続きは非常に長くかかり得ることに注意してください。
[R7RS charset] {scheme.charset} char-setに含まれる文字をリストあるいは文字列にして返します。 大きな文字集合に適用する場合は注意してください。
Previous: 文字集合への問い合わせ, Up: R7RS文字集合 [Contents][Index]
[R7RS charset] {scheme.charset} char-setにchar1 …を加えた文字集合を返します。
線形更新版のchar-set-adjon!
はchar-set引数を破壊的に再利用するかもしれません。
[R7RS charset] {scheme.charset} char-setからchar1 …を除いた文字集合を返します。 char1 …がchar-setに含まれている必要はありません。
線形更新版のchar-set-delete!
はchar-set引数を破壊的に再利用するかもしれません。
[R7RS charset] {scheme.charset} char-set …内の少なくともひとつに含まれる文字の文字集合を返します。 引数が無ければ空の文字集合を返します。
線形更新版のchar-set-union!
はchar-set1引数を破壊的に再利用するかもしれません。
[R7RS charset]
{scheme.charset}
char-set …の全てに共通して含まれる文字の文字集合を返します。
引数が無ければchar-set:full
を返します。
線形更新版のchar-set-intersection!
はchar-set1引数を破壊的に再利用するかもしれません。
[R7RS charset]
{scheme.charset}
char-set1に含まれ、しかしchar-set2
…のどれにも含まれない文字の文字集合を返します。
線形更新版のchar-set-difference!
はchar-set1引数を破壊的に再利用するかもしれません。
[R7RS charset]
{scheme.charset}
引数がない場合は空の文字集合を、引数が一つの場合はそれをそのまま返します。
引数が二つの場合、そのどちらか一方に含まれるが両方には含まれない文字の文字集合を返します。
引数が三つ以上の場合は(char-set-xor (char-set-xor set1 set2) set3 …)
を
返します。
線形更新版のchar-set-xor!
はchar-set1引数を破壊的に再利用するかもしれません。
[R7RS charset]
{scheme.charset}
(char-set-difference char-set1 char-set2 …
の結果と
(char-set-intersection char-set1 char-set2 …
の結果の
二つの値を返します。
Next: R7RS変更不可リスト, Previous: R7RS文字集合, Up: R7RS large [Contents][Index]
scheme.hash-table
- R7RSハッシュテーブルこのモジュールは、元々srfi-125
で定義されていたハッシュテーブルのライブラリです。
このモジュールで提供されるハッシュテーブルはGaucheの組み込みハッシュテーブルそのものです。 ただ、srfi-125はいくつか、Gaucheの元々持っていた手続きと名前が衝突する手続きを 定義しています。Gaucheはそれらの手続きに関しては異なる名前で組み込み手続きを 用意しています。組み込みのハッシュテーブル手続きについては ハッシュテーブルを参照してください。
このモジュールを使うと、手続きはR7RSで定義されたとおりの動作になります。 ポータブルなコードを書く際にこのモジュールを使うと良いでしょう。
srfi-125はまた、srfi-69で定義されたいくつかの手続きを、非推奨ながら 互換性のためにサポートしています。これらの非推奨手続きも このモジュールで提供されます。
以下の手続きはGaucheの組み込みと同じです。
hash-table? hash-table-contains? hash-table-exists? hash-table-empty? hash-table=? hash-table-mutable? hash-table-ref hash-table-ref/default hash-table-set! hash-table-update!/default hash-table-clear! hash-table-size hash-table-keys hash-table-values hash-table-copy hash-table-empty-copy hash-table->alist hash-table-union! hash-table-intersection! hash-table-difference! hash-table-xor!
これらの手続きの説明はハッシュテーブルを参照してください。
以下の手続きは、Gaucheの組み込みでは-r7
サフィックスをつけた名前で
提供されています。
hash-table hash-table-delete! hash-table-intern! hash-table-update! hash-table-pop! hash-table-find hash-table-count hash-table-map hash-table-for-each hash-table-map! hash-table-map->list hash-table-prune!
[R7RS hash-table]
{scheme.hash-table}
この手続きは、組み込みのmake-hash-table
を拡張して二番目の形式を許したものです。
二番目の形式は比較器のかわりに二つの手続きをとるもので、srfi-69互換です。
srfi-69互換形式では、equal-procはふたつのキーを取りそれが等しいかどうかを 返す述語、hash-procは一つのキーを取りそのハッシュ値を非負の正確な整数で 返す手続きです。 この互換係式は非推奨で、新たなコードでは使うべきではありません。
省略可能なarg dots引数は実装依存で、作られるハッシュテーブルの 様々な特性を指定するものです。Gaucheでは無視されます。
[R7RS hash-table]
{scheme.hash-table}
これは組み込みのhash-table-r7
と同じです(ハッシュテーブル参照)。
キーの比較とハッシュに比較器cmprを使う新たなハッシュテーブルを作って返します。 また、ハッシュテーブルの内容はkey value …によって初期化されます。 ここで引数はキーと値が交互に並んだものです。key value …部分の 長さが偶数でなければエラーが投げられます。
srfi-125では、この手続きは(実装がサポートしていれば)変更不可なハッシュテーブルを 返すと規定されています。Gaucheは変更不可なハッシュテーブルを持っていないので、 変更可能なハッシュテーブルが返りますが、ポータブルなコードを書く際には 返されたテーブルを変更しないようにしてください。 なお、Gaucheも変更不可なマップは持っています (変更不可なマップ参照)。
[R7RS hash-table]
{scheme.hash-table}
この手続きは、組み込みのalist->hash-table
を拡張して二番目の形式を許したものです。
二番目の形式は比較器のかわりに二つの手続きをとるもので、srfi-69互換です。
srfi-69互換形式では、equal-procはふたつのキーを取りそれが等しいかどうかを 返す述語、hash-procは一つのキーを取りそのハッシュ値を非負の正確な整数で 返す手続きです。 この互換係式は非推奨で、新たなコードでは使うべきではありません。
省略可能なarg dots引数は実装依存で、作られるハッシュテーブルの 様々な特性を指定するものです。Gaucheでは無視されます。
[R7RS hash-table]
{scheme.hash-table}
省略可能なarg …を許すことを除いて、
Gauche組み込みのhash-table-unfold
と同じです。
arg …は作られるテーブルのパラメータを与える実装依存な引数で、
Gaucheでは今のところ無視されます。
詳しくはハッシュテーブルを参照してください。
[R7RS hash-table]
{scheme.hash-table}
これは組み込みのhash-table-delete!-r7
と同じです。
ハッシュテーブルhtから、key …を持つエントリを削除します。 htがkeyを持っていなくてもエラーにはなりません。 実際に削除されたエントリの数を返します。
組み込みのhash-table-delete!
と違うのは次の2点です。
組み込みはkeyをひとつしか取らず、そえが実際に削除されたかどうかをブール値で返します。
[R7RS hash-table]
{scheme.hash-table}
これは組み込みのhash-table-intern!-r7
と同じです。
ハッシュテーブルhtからキーkeyを持つエントリを探し、
見つかった場合はその値を返します。見つからなかった場合は、手続きfailureを
引数無しで呼び出し、その返り値をkeyに結びついた新たなエントリとして
htに追加し、またhash-table-intern!
の返り値とします。
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-update!-r7
.
It takes different optional arguments from built-in hash-table-update!
.
Updater is a procedure that takes one argument, failure is a thunk, and success is a procedure that takes one argument.
Works the same as follows, except maybe more efficiently.
(hash-table-set! ht key (updater (hash-table-ref ht key failure success)))
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-pop!-r7
.
It is a completely different procedure as built-in hash-table-pop!
.
Removes an arbitrary entry in the hash table ht, and returns the removed entry’s key and value as two values.
If ht is empty, an error is signalled.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-find-r7
.
It takes different arguments from built-in hash-table-find
.
Calls proc with a key and a value of each entry in ht,
until proc returns non-false value. If proc returns
non-false value, hash-table-find
immediately returns it.
If proc returns #f
for all entries, calls a thunk
failure and returns its result.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-count-r7
.
Calls proc with a key and a value of each entry in ht, and returns the number of times when proc returned true.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-map-r7
.
This is different from built-in hash-table-map
.
Creates a fresh hashtable with a key comparator cmpr, then populate it by inserting the key and the result of applying proc on the value of each entry in ht.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-map!-r7
.
Calls proc on the value of each entry in ht, and update the entry with the result of proc.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-map->list-r7
,
and same as built-in hash-table-map
(not the scheme.hash-table
’s
hash-table-map
) except the order of the arguments.
Apply proc on a key and a value of each entry in ht, in arbitrary order, and returns a list of results.
[R7RS hash-table] {scheme.hash-table} Apply proc on a key and a value of each entry in ht. The result of proc is discarded. Returns an unspecified value.
This procedure allows arguments in both order for the compatibility—
the first way is the scheme.hash-table
recommended one,
which is the same as built-in hash-table-for-each-r7
,
and the latter way is compatible with srfi-69, which is
the same as built-in hash-table-for-each
.
It is unfortunate that this compatibility thing is extremely confusing; especially in Gauche, you can make anything applicable, so the distinction between procedures and other objects is blurred.
We recommend that you stick to one way or another within a module; if
your module uses built-in interface, use (hash-table-for-each ht proc)
.
If your module imports scheme.hash-table
,
use (hash-table-for-each proc ht)
.
[R7RS hash-table] {scheme.hash-table} The proc argument takes three arguments, a key, a value, and the current seed value. The procedure applies proc for each entry in ht, using seed as the first seed value, and using the previous result of proc as the subsequent seed value. Returns the result of the last call of seed.
This procedure allows arguments in both order for the compatibility—
the first way is the scheme.hash-table
recommended one,
which is the same as built-in hash-table-fold-r7
,
and the latter way is compatible with srfi-69, which is
the same as built-in hash-table-fold
.
It is unfortunate that this compatibility thing is extremely confusing.
We recommend that you stick to one way or another within a module; if
your module uses built-in interface, use the second interface.
If your module imports scheme.hash-table
,
use the first interface.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-prune!-r7
.
Apply proc on a key and a value of each entry in ht, and deletes the entry if proc returns a true value. This procedure returns an unspecified value.
[R7RS hash-table]
{scheme.hash-table}
This is the same as hash-table-union!
, and provided just
for the compatibility with srfi-69. Deprecated.
[R7RS hash-table] {scheme.hash-table} Provided for the compatibility with srfi-69, and are deprecated.
The first three are the same as built-in default-hash
,
string-hash
, and string-ci-hash
, except that
these accept an optional second argument, which is ignored.
Note that hash-by-identity
is also defined as the same
as default-hash
except the ignored optional second argument,
per srfi-125, although the name suggests that it would work as if
eq-hash
.
Do not use these procedures in the new code; you can use comparators
instead (default-comparator
, string-comparator
,
string-ci-comparator
and eq-comparator
,
see 用意されている比較器). If you do need hash function,
you should still avoid hash
and hash-by-identity
,
and use default-hash
and eq-hash
instead.
[R7RS hash-table] {scheme.hash-table} Provided for the compatibility with srfi-69, and are deprecated.
Returns the equivalence function and hash function of a hash table ht.
For the introspection, we recommend to use
built-in hash-table-comparator
. (Unfortunately, it is not
included in scheme.hash-table
, though.)
Next: R7RSランダムアクセスリスト, Previous: R7RSハッシュテーブル, Up: R7RS large [Contents][Index]
scheme.ilist
- R7RS変更不可リストThis module provides a set of procedures that handles immutable pairs and lists.
The standard allows an implementation to have mutable pairs and immutable
pairs separately, so it defines immutable version of most scheme.list
procedures. In Gauche, mutable pairs and immutable pairs are both
abstract “pairs”, and all procedures that accesses pairs (without modifying
them) works seamlessly on both kind.
Consequently, the following procedures are just aliases of their
non-immutable versions (just remove i
prefix from them).
Note that these procedures and variables in Gauche do not reject if the input is
mutable pairs/lists, but such usage may not be portable.
proper-ilist? ilist? dotted-ilist? not-ipair? null-ilist? ilist= icar icdr icaar icadr icdar icddr icaaar icaadr icadar icaddr icdaar icdadr icddar icdddr icaaaar icaaadr icaadar icaaddr icadaar icadadr icaddar icadddr icdaaar icdaadr icdadar icdaddr icddaar icddadr icdddar icddddr icar+icdr ilist-ref ifirst isecond ithird ifourth ififth isixth iseventh ieighth ininth itenth idrop ilist-tail itake-right ilast last-ipair ilength icount ifor-each ifold ipair-fold ireduce ifold-right ipair-fold-right ireduce-right ipair-for-each imember imemv imemq ifind-tail iany ievery ilist-index idrop-while iassoc iassq iassv iapply make-ilist-comparator make-improper-ilist-comparator make-icar-comparator make-icdr-comparator
Next: R7RS変更不可な両端キュー, Previous: R7RS変更不可リスト, Up: R7RS large [Contents][Index]
scheme.rlist
- R7RSランダムアクセスリストThis module provides an alternative datatype for pairs and lists,
which are immutable and allows O(log n) random access, while
maintaining O(1) car
and cdr
operations.
This is originally defined as srfi-101
. We call this datatype
rlist in the following explanation.
The srfi allows a Scheme implementation to adopt rlist
as the built-in pairs and lists, so the procedure names are
duplicated from the Scheme primitives. If you use this module,
you might want to import with prefix
(e.g. (use scheme.rlist :prefix rlist:)
).
In Gauche, we implement rlist on top of skew-list
(see Skew binary random-access lists). The main difference
is that skew-list only allows proper lists, while rlist allows
improper lists (the cdr
of the last pair can be an arbitrary
objects).
However, having improper lists makes things a lot complicated. If you just need a list with fast random access, you want to use skew-list. Use rlist only if you need to deal with improper lists.
The following procedures behave just like the built-in counterparts,
except they take/return rlists instead of ordinary pairs and lists.
(NB: list-ref
and list-tail
here doesn’t take the optional
fallback argument, for they are Gauche’s extension).
pair? cons car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr null? list? list make-list length append reverse list-tail list-ref map for-each
This module also exports a syntax quote
, which denotes
the literal rlist. (quote (a (b c)))
becomes a literal
rlist consists of elements a
, and an rlist consists
of b
and c
.
Note that if you import scheme.rlist
without suffix,
the shorthand notation '(a (b c))
refers to scheme.rlist#quote
,
instead of built-in quote
.
Returns a new rlist which is the same as rlist except that its k-th element is replaced with obj. This is O(log n) operation where n is the length of rlist.
Returns two values, the result of (list-ref rlist k)
and the result of (list-set rlist k (proc (list-ref rlist k)))
,
but may be more efficient.
Convert a proper rlist to an ordinary list. An error is thrown when rlist is not proper. The conversion is only done in the “spine” of rlist; that is, if rlist is nested, only the outermost rlist is converted.
Convert a proper ordinary list to an rlist. An error is thrown when list is not proper. The conversion is only done in the “spine” of list; that is, if list is nested, only the outermost list is converted.
Next: R7RS変更不可なテキスト, Previous: R7RSランダムアクセスリスト, Up: R7RS large [Contents][Index]
scheme.ideque
- R7RS変更不可な両端キューThis module provides a functional double-ended queue (deque, pronounced as “deck”), with amortized O(1) access of queue operations on either end.
It also serves as a convenient bidirectional list structures in a sense that operations from the end of the list is just as efficient as the ones from the front.
Note: If you don’t need immutability and wants space-efficient
deque, you can also use data.ring-buffer
as a deque
(see リングバッファ).
This module was originally defined as srfi-134
, then
became a part of R7RS large.
Gauche’s data.ideque
is a superset of this module.
See 変更不可な両端キュー.
[R7RS ideque] {scheme.ideque} Returns an ideque with the given elements.
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
[R7RS ideque] {scheme.ideque}
Next: R7RSジェネレータ, Previous: R7RS変更不可な両端キュー, Up: R7RS large [Contents][Index]
scheme.text
- R7RS変更不可なテキストThis module provides text type, an immutable string with O(1) random access. This is originally defined in srfi-135.
In Gauche, a string with index access generally takes O(n); however,
if the string is ASCII-only or you precalculate string indexes,
index access becomes O(1) (see 文字列索引).
So, a text in Gauche is just an immutable indexed string. It satisfies
string?
, too.
Note that, in Gauche, text is not disjoint from string, so textual
type is just a string type, and many textual-*
procedures are
just aliases to the corresponding string-*
procedures.
Use this module if you need portability.
[R7RS text]
{scheme.text}
Returns #t
iff obj is a text, which is an immutable
indexed string in Gauche. Note that a text type is not disjoint
from a string in Gauche.
[R7RS text]
{scheme.text}
Returns #t
iff obj is either a text or a string.
In Gauche, this is just an alias of string?
.
[R7RS text]
{scheme.text}
Returns #t
iff obj is an empty string/text. In Gauche
this is just an alias of string-null?
.
[R7RS text] {scheme.text}
[R7RS text]
{scheme.text}
In Gauche it is same as make-string
, except that the returned
string is immutable and indexed, and you can’t omit char.
[R7RS text]
{scheme.text}
Like string
, but returned string is immutable and indexed.
[R7RS text]
{scheme.text}
Like string-tabulate
, but returned string is immutable and indexed
(see 文字列の作成).
[R7RS text]
{scheme.text}
Like string-unfold
and string-unfold-right
,
but returned string is immutable and indexed
(see 文字列の作成). The mapper procedure f may return
a string instead of a character.
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text} Takes a string, and returns an immutable, indexed string of the same content. If textual is already a such string, it is returned as is.
[R7RS text]
{scheme.text}
Converts a textual to a fresh mutable string,
a vector and a list, respectively.
These are aliases of string-copy
, string->vector
and string->list
in Gauche.
[R7RS text]
{scheme.text}
Convert a string, a vector of characters, and a list of characters
to an immutable indexed string.
If an immutable indexed string is given to string->text
without
start/end arguments, the input is returned as is.
[R7RS text]
{scheme.text}
Same as (list->text (reverse char-list))
but maybe more efficient.
[R7RS text]
{scheme.text}
Returns a bytevector that contains utf8 encoding of the input
textual. In Gauche, it is the same as string->utf8
(see Unicode transfer encodings).
[R7RS text]
{scheme.text}
Returns a bytevector that contains utf16 encoding of the input
textual, with BOM attached, and in the native byteorder.
In Gauche, it is the same as
(string->utf16 textual (native-endian) #t [start end])
(see Unicode transfer encodings).
[R7RS text]
{scheme.text}
Returns a bytevector that contains utf16be and utf16le encoding of the input
textual. No BOM is attached.
In Gauche, they are the same as
(string->utf16 textual 'big-endian #f [start end])
and
(string->utf16 textual 'little-endian #t [start end])
(see Unicode transfer encodings).
[R7RS text] {scheme.text} Converts a utf8 octet sequence stored in bytevector to a text. If the octet sequence begins with BOM sequence, it is interpreted as a character U+FEFF. Optional start/end arguments limits the input range of bytevector.
If the input contains an invalid utf-8 sequence, and Gauche’s native encoding is utf-8, it is replaced by a unicode replacement character U+FFFD. (NB: Srfi-135 says it is an error.)
[R7RS text]
Converts a utf16 octet sequence stored in bytevector to a text.
For utf16->text
, the sequence may begin with BOM,
in which case it determines the endianness.
Otherwise, platform’s native endianness is assumed.
Fro utf16be->text
and utf16le->text
, the input is
assumed to be in UTF16BE/UTF16LE respectively; if it begins with BOM
it is treated as a character U+FEFF.
Optional start/end arguments limits the input range of bytevector.
If the length of input (end - start, if the range is limited) isn’t even, an error is thrown.
If the input contains an invalid utf-16 sequence (unpaired surrogates), it is replaced by a unicode replacement character U+FFFD. (NB: Srfi-135 says it is an error.) {scheme.text}
[R7RS text] {scheme.text} Returns a text between start-th (inclusive) and end-th (exclusive) characters in the input.
[R7RS text] {scheme.text} Returns a copy of textual, optionally limited between start/end.
Srfi-135 specifies that even the input is a text, the returned one must be freshly allocated. In Gauche, string body is immutable anyway, so it isn’t a big deal.
[R7RS text]
{scheme.text}
In Gauche, these are the same as corresponding string operations
(string-take
, string-drop
, string-take-right
,
string-drop-right
, string-pad
, string-pad-right
,
string-trim
, string-tirm-right
, and string-trim-both
),
except that the returned string is always immutable and indexed.
See 文字列の選択, for these string procedures.
[R7RS text]
{scheme.text}
In Gauche, this is same as string-replace
,
except that the returned string is always immutable and indexed.
See 他の文字列操作, for the details.
[R7RS text]
{scheme.text}
In Gauche, these are just aliases of built-in
string=?
, string<?
, string>?
,
string<=?
and string>?
, respectively.
See 文字列の比較, for the details.
[R7RS text]
{scheme.text}
In Gauche, these are just aliases of the unicode version of
string-ci=?
, string-ci<?
, string-ci>?
,
string-ci<=?
and string-ci>?
, respectively.
See フルセットの大文字小文字変換, for the details.
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text] {scheme.text}
[R7RS text]
{scheme.text}
Like string-split
, except that the returned strings are all
immutable and indexed. See 文字列を扱うその他の手続き, for the details.
Next: R7RS遅延シーケンス, Previous: R7RS変更不可なテキスト, Up: R7RS large [Contents][Index]
scheme.generator
- R7RSジェネレータこのモジュールは、ジェネレータとアキュムレータを提供します。
これらは最初にsrfi-121で定義され、srfi-158で拡張された後、
(scheme generator)
としてR7RS largeに取り込まれました。
ジェネレータは引数と取らない手続きで、呼ばれる度に値を次々に返し、
EOFを以って終端を示すものです。ジェネレータを扱う手続きは
srfi-121のスーパーセットであるgauche.generator
で提供されています。
詳しくはジェネレータを参照してください。
アキュムレータはジェネレータの反対で、値を消費する手続きと考えられます。 アキュムレータは一つの引数を取ります。EOF以外の値が与えられた場合は、 それが内部に何らかの形で蓄えられ、EOFが与えられた場合、 蓄えられた値が返されます。 どのように値が蓄えられるかは各アキュムレータに依存します。
一度EOFが与えられると、アキュムレータは終了状態になります。 その後何度EOFが与えられても同じ蓄積結果を返します。 終了状態になったアキュムレータにEOF以外の値を与えた場合の動作は未定義です。
アキュムレータは、複合的なオブジェクトを生成する手続きで、
実際に生成される値の型をパラメタライズするのに使えます。
次の手続きを見てみましょう。これは二つのジェネレータから交互に値を取り、
それを蓄積して返す手続きです。
(glet*
はGaucheの手続きですがsrfi-158には含まれていません。)
(define (intertwine acc gen1 gen2) (let loop () (glet* ([a (gen1)] [b (gen2)]) (acc a) (acc b) (loop))) (acc (eof-object)))
この手続きは具体的に返される値が何かは関知しません。 それは引数に与えられたアキュムレータにより決定されます。
(intertwine (list-accumulator) (giota 5) (giota 5 100)) ⇒ (0 100 1 101 2 102 3 103 4 104) (intertwine (vector-accumulator) (giota 5) (giota 5 100)) ⇒ #(0 100 1 101 2 102 3 103 4 104) (intertwine (bytevector-accumulator) (giota 5) (giota 5 100)) ⇒ #u8(0 100 1 101 2 102 3 103 4 104)
註: Gaucheでは、クラスを使うことで戻り値となるコンテナの型をパラメタライズできます
(例: map-to
)。これは多くのコレクションクラスがbuilderプロトコルを
サポートしているからです。詳しくはコレクションフレームワークを見てください。
アキュムレータの利点は、同じ型の戻り値についても、異なる値の蓄積方法
(例えばリストを順に蓄積するか逆順に蓄積するか、等)を提供できることです。
以下のジェネレータ手続きは、gauche.generator
のセクションで
解説してあります(ジェネレータ参照)。
generator circular-generator make-iota-generator make-range-generator make-coroutine-generator make-unfold-generator make-for-each-generator list->generator vector->generator reverse-vector->generator string->generator bytevector->generator
gcons* gappend gflatten ggroup gmerge gmap gcombine gfilter gremove gstate-filter ggroup gtake gdrop gtake-while gdrop-while gdelete gdelete-neighbor-dups gindex gselect
generator->list generator->reverse-list generator-map->list generator->vector generator->vector! generator->string generator-count generator-any generator-every generator-unfold
generator-fold generator-for-each generator-find
以下はアキュムレータ手続きです。
[R7RS generator]
{scheme.generator}
内部状態をひとつ持つアキュムレータを作って返します。
状態はkhilで初期化されます。
EOFでない値vがアキュムレータに与えられる度に、手続きkonsが
(kons v state)
と呼ばれ、その戻り値が新たな状態となります。
EOFがアキュムレータに渡されると、(finalizer state)
が呼ばれ、
その結果がアキュムレータの戻り値となります。
[R7RS generator]
{scheme.generator}
蓄積された値をリストとして返すアキュムレータを作って返します。
list-accumulator
ではリストは与えられた順に、
reverse-list-accumulator
では逆順になります。
[R7RS generator]
{scheme.generator}
蓄積された値をベクタあるいはバイトベクタ(u8vector)として返すアキュムレータを
作って返します。vector-accumulator
と
bytevector-accumulator
では値は先頭から順に、
reverse-vector-accumulator
ではベクタの最後から逆順に詰められます。
reverse-bytevector-accumulator
はありません。
[R7RS generator] {scheme.generator} vecとbvecはそれぞれ変更可能なベクタとバイトベクタ(u8vector)です。 この引数はバッファとして使われます。
与えられた値を、バッファのインデックスatから順に格納してゆくアキュムレータを 作って返します。バッファの最後を越えて値を格納しようとした場合はエラーになります。
EOFが与えられたら、vecまたはbvecをそのまま返します。
[R7RS generator] {scheme.generator} 文字を受け取り、それを蓄積して最後に文字列として返すアキュムレータを生成して返します。
[R7RS generator] {scheme.generator} スカラー値を生成するアキュムレータを作って返します。
sum-accumulator
とproduct-accumulator
で作られた
アキュムレータはそれぞれ、
数値を受け取り、それを蓄積された値に加算または乗算してゆきます。
値の初期値はそれぞれ0と1です。
count-accumulator
で作られたアキュムレータは
任意のオブジェクトを受け取ります。
EOFを受け取ったらそれまで受け取ったオブジェクトの数を返します。
Next: R7RSストリーム, Previous: R7RSジェネレータ, Up: R7RS large [Contents][Index]
scheme.lseq
- R7RS遅延シーケンスThis module provides lightweight lazy sequence (lseq), conceptually represented by a pair of element and generator. When the rest of sequence is taken, the generator is evaluated and yields another pair of element and generator, and so on. The overhead is one allocation of a pair per element. It is much lighter than streams (see ストリームライブラリ), which requires to create a thunk for every element.
Gauche already has built-in support for such lazy sequences; we go further to make it behave like ordinary pairs—that is, if you take cdr of a lazy pair, we automatically forces the generator so it is indistinguishable from an ordinary pair, modulo side effects. See 遅延シーケンス.
Srfi-127, the original srfi for this module,
is a bit ambiguous whether its lazy sequence must be
implemented with a pair whose cdr is a generator procedure, or
it refers to the pair+generator as a conceptual model. Considering
of the purpose of lazy sequence, the concrete implementation shouldn’t
matter; that is, the user of lazy sequence should not count on the fact
that the lseq is an improper list terminated by a generator procedure.
Instead, an lseq should be treated as an opaque object that can be
passed to scheme.lseq
procedures.
With that premise, we implement this module as just a thin wrapper of
Gauche’s native lazy sequence. It is upper-compatible,
except that the code that assumes the internal structure could break.
Notably, the constructor generator->lseq
is the same as
Gauche’s built-in, which returns Gauche’s lseq, undistinguishable
to the ordinary list.
(procedure? (generator->lseq (generator 1))) ;; => #t, in srfi-127 reference implementation, ;; #f, in our implementation.
[R7RS lseq]
{scheme.lseq}
Returns true iff x is an object that can be passed to
lseq procedures. In Gauche, it returns #t
if x is
a pair or an empty list, since a lazy pair is indistinguishable
from a pair.
[R7RS lseq]
{scheme.lseq}
Compare two lseqs element-wise using elt=?
and returns #t
iff two lseqs are equal.
[R7RS lseq]
{scheme.lseq}
Returns the first item of lseq. If lseq is empty, an error
is raised. In Gauche, these are just aliases of car
.
[R7RS lseq]
{scheme.lseq}
Returns the rest of lseq. If lseq is empty, an error
is raised. In Gauche, these are just aliases of cdr
.
[R7RS lseq]
{scheme.lseq}
Returns an lseq that has first k
items,
or an lseq that skips first k
items, respectively.
An error is signaled when the resulting lseq of lseq-take
reached at the end of sequence before k
items are taken.
It is different from Gauche’s ltake
, which simply returns ()
in such case.
On the other hand, lseq-drop
is the same as drop
in Gauche; it just drops k items from the head of input
sequence, regardless of whether it is an ordinary list or lseq.
[R7RS lseq] {scheme.lseq} Realizes all the elements in lseq, resulting an ordinary list.
[R7RS lseq]
{scheme.lseq}
Creates a generator from lseq. In Gauche, this is same
as list->generator
.
[R7RS lseq]
{scheme.lseq}
Returns the length of lseq. All the elements in lseq are
realized as the side effect. In Gauche, this is same as length
.
[R7RS lseq]
{scheme.lseq}
Append one or more lseqs lazily. This is the same as lappend
in Gauche.
[R7RS lseq]
{scheme.lseq}
Returns a lazy sequence in which the first element is a list of
first elements of lseq
s, and so on.
[R7RS lseq]
{scheme.lseq}
Lazy map. The same as Gauche’s lmap
. Returns a lazy sequence.
[R7RS lseq]
{scheme.lseq}
This one consumes all the input lseqs, applying proc on
each corresponding elements of the input sequences for the side
effects. In Gauche, it is the same as for-each
, for
Gauche doesn’t distinguish lseqs and ordinary lists.
[R7RS lseq]
{scheme.lseq}
Returns an lseq that contains elements from the input
lseq that satisfy or don’t satisfy pred, respectively.
Lseq-filter
is the same as Gauche’s lfilter
.
[R7RS lseq]
{scheme.lseq}
These are the same as Gauche’s ltake-while
and drop-while
(the latter doesn’t have l
-prefix, since it just drops items
from the head of the input sequence, regardless of whether it is
an ordinary list or an lseq.
[R7RS lseq]
{scheme.lseq}
In Gauche, these are the same as the corresponding list functions,
find
, find-tail
, any
, every
,
list-index
, member
, memq
and memv
, respectively,
for all of those functions won’t look at input more than necessary
so lseqs work just as well as ordinary lists.
Next: R7RSボックス, Previous: R7RS遅延シーケンス, Up: R7RS large [Contents][Index]
scheme.stream
- R7RSストリームThis module provides utilities for lazily evaluated streams. It is more heavyweight than lazy sequences (see 遅延シーケンス), but it strictly implements “as lazy as possible” semantics—elements are never evaluated until it is actually accessed.
The following procedures are provided in Gauche’s util.stream
module;
see ストリームライブラリ for their description:
stream-null stream-cons stream? stream-null? stream-pair? stream-car stream-cdr stream-lambda define-stream list->stream port->stream stream->list stream-append stream-concat stream-constant stream-drop-while stream-filter stream-fold stream-for-each stream-from stream-iterate stream-length stream-let stream-map stream-match stream-of stream-range stream-ref stream-reverse stream-scan stream-take-while stream-unfold stream-unfolds stream-zip
The following macro and procedures have different interface from
Gauche’s util.stream
module:
stream stream-take stream-drop
[R7RS stream] {scheme.stream} Returns a new stream whose elements are the result of expr …. Arguments won’t be evaluated until required.
This differs from srfi-40 and util.stream
’s stream
,
which is a procedure so arguments are evaluated
(see Stream constructors, for the details).
[R7RS stream]
{scheme.stream}
Returns a stream that contains first n elements from stream,
or elements without first n elements from it, respectively.
If stream has less than n elements, stream-take
returns a copy of the entire stream, while stream-drop
returns a null stream.
Note that the argument order doesn’t follow the Scheme tradition,
which takes the main object (stream in this case) first, then
the count. Procedures with the same name is provided in util.stream
with the different argument order.
Next: R7RSリストキュー, Previous: R7RSストリーム, Up: R7RS large [Contents][Index]
scheme.box
- R7RSボックスGauche supports boxes built-in (see ボックス), so this module is merely a facade that exports the following identifiers:
box box? unbox set-box!
Next: R7RS Ephemeron, Previous: R7RSボックス, Up: R7RS large [Contents][Index]
scheme.list-queue
- R7RSリストキューリストを元にしたキューのライブラリです。
Gaucheはdata.queue
モジュールでキューをサポートしています
(スレッドセーフなキューも含まれています。詳しくはキューを参照。)
このモジュールは、主としてポータブルなコードのために、
data.queue
の<queue>
の上に実装されています。
このモジュールでいうリストキューは<queue>
のインスタンスそのものなので、
make-queue
で作ったキューをscheme.list-queue
のAPIに渡したり、
make-list-queue
で作ったキューをGaucheのキューAPIに渡すこともできます。
註: このライブラリのAPIには、性能のために、 キューが使う内部のペアをそのまま返すことを要求されている ものがあります。これらの内部ペアはキューの操作によって破壊的に変更される可能性があります。 ユーザ側でも変更した場合、その後のキューの動作は保証されません。
[R7RS list-queue]
{scheme.list-queue}
リストlisの内容を初期値として持つリストキューを作って返します。
Gaucheでは、リストキューは<queue>
のインスタンスです(キュー参照)。
lisの実体は作成されたキューの所有物となります。 呼び出し側は、この関数を呼んだ後でlisを変更してはいけません。 また、作成されたキューの操作によってlisの内容は変更されるでしょう。
省略可能なlast引数は、もし与えられる場合は、lisの最後のペアで
なければなりません。この引数が渡された場合、make-list-queue
は自分で
リストの末尾のペアを見つけるかわりに、呼び出し側を信頼してlastを最後の
ペアとして保持します。
[R7RS list-queue]
{scheme.list-queue}
elt …を初期内容とするリストキューを作成して返します。
Gaucheでは、リストキューは<queue>
のインスタンスです(キュー参照)。
[R7RS list-queue] {scheme.list-queue} リストキューqueueのコピーを返します。
[R7RS list-queue]
{scheme.list-queue}
リストキューqueueの前に、(unfold p f g seed)
で生成される
要素を付け足して、queueを返します。
unfold
についてはR7RSリストを参照してください。
queueが省略された場合は新たに作成したキューを使います。
(list-queue-unfold (pa$ = 5) ; p
(pa$ * 2) ; f
(pa$ + 1) ; g
0 ; seed
(list-queue 'x 'y 'z))
⇒ a queue containing (0 2 4 6 8 x y z)
[R7RS list-queue]
{scheme.list-queue}
queueの後ろに、(unfold-right p f g seed)
で生成される
要素を付け足して、queueを返します。
unfold-right
についてはR7RSリストを参照してください。
queueが省略された場合は
新たに作成したキューを使います。
(list-queue-unfold-right (pa$ = 5) ; p
(pa$ * 2) ; f
(pa$ + 1) ; g
0 ; seed
(list-queue 'x 'y 'z))
⇒ a queue containing (x y z 8 6 4 2 0)
[R7RS list-queue]
{scheme.list-queue}
queueがリストキューなら#t
を、そうでなければ#f
を返します。
Gaucheではdata.queue
モジュールのqueue?
と同じです。
[R7RS list-queue]
{scheme.list-queue}
queueが空なら#t
を、そうでなければ#f
を返します。
data.queue
のqueue-empty?
と同じです。
[R7RS list-queue]
{scheme.list-queue}
queueの先頭の要素を返します。queueが空の場合はエラーを報告します。
data.queue
のqueue-front
と同じです。
[R7RS list-queue]
{scheme.list-queue}
queueの末尾の要素を返します。queueが空の場合はエラーを報告します。
data.queue
のqueue-rear
と同じです。
[R7RS list-queue] {scheme.list-queue} queueが内部的に保持している要素のリストを返します。 返されたリストは、queueが操作されれば破壊的に変更される可能性があり、 また返されたリストを破壊的に変更した場合はqueueの一貫性が失われます。 この手続きの主な目的は、他のキュー操作を効率よく実装することです。
単にキューの中身にオーバーヘッド無くアクセスしたい場合は、
list-queue-remove-all!
が使えないかどうか検討してください。
そちらはキューの中身のリストを直接返すと同時にキュー自体を空にするので、安全です。
[R7RS list-queue] {scheme.list-queue} queueの内部で要素を保持しているリストの先頭と末尾のペアを返します。 キューが空の場合は、二つの空リストを返します。
この手続きも、list-queue-list
と同じく、内部のリストを直接返すため、
返されたリストは、queueが操作されれば破壊的に変更される可能性があり、
また返されたリストを破壊的に変更した場合はqueueの一貫性が失われます。
この手続きの主な目的は、他のキュー操作を効率よく実装することであり、
一般的な用途に使うべきではありません。
[R7RS list-queue]
{scheme.list-queue}
eltをqueueの先頭に追加します。
data.queue
の(queue-push! queue elt)
と同じです。
[R7RS list-queue]
{scheme.list-queue}
eltをqueueの末尾に追加します。
data.queue
の(enqueue! queue elt)
と同じです。
[R7RS list-queue]
{scheme.list-queue}
queueの先頭から要素をひとつ取り除き、その取り除かれた要素を返します。
queueが空ならエラーを報告します。
data.queue
の(dequeue! queue elt)
と同じです。
[R7RS list-queue] {scheme.list-queue} queueの末尾から要素をひとつ取り除き、その取り除かれた要素を返します。 queueが空ならエラーを報告します。 この手続きは、キューの持つ要素数nに対してO(n)の時間がかかります。 もしこの操作を頻繁に必要とするなら、デック(deque, 両端キュー)を使うことを 検討すべきでしょう。(変更不可な両端キューおよび リングバッファ参照。)
[R7RS list-queue]
{scheme.list-queue}
queueを空にして、入っていた全ての要素をリストで返します。
リストはコピーされません。つまりO(1)の操作です。
可能なら、list-queue-list
よりはこちらを使う方が安全です。
Gaucheでは、これはdata.queue
のdeque-all!
と同じです。
[R7RS list-queue] {scheme.list-queue} queueを変更して、lisの内容がキューの内容になるようにします。 queueの元の内容は捨てられます。省略可能なlast引数が渡される場合、 それはlisの最後のペアでなければなりません。手続きは呼び出し元を信頼して、 lisをスキャンせずにlastを最後のペアと考えることで O(1)操作を実現しています。
この手続きを呼び出した後では、lisはqueueに所有され、破壊的変更を 受けます。この後でlisの内容をあてにしたり変更したりしてはいけません。
[R7RS list-queue] {scheme.list-queue} 与えられたqueueを全部つないだ要素を持つリストキューを 新たに作成して返します。引数のキューは変更されません。 これは要素の総数nについてO(n)の操作となります。
[R7RS list-queue]
{scheme.list-queue}
与えられたqueueの要素を全てつないだものを要素とするリストキューを
返します。操作によって、queueの内容は破壊されることがあり、
以降それらのキューを使ってはなりません
(Gaucheでは、事故を避けるために、全てのqueueを空にしています)。
結果として返されるキューが、引数のどれともeq?
になる必要はない、
ということに注意してください。
これはキューの総数mに対してO(m)の操作です(要素の総数ではなく)。
[R7RS list-queue]
{scheme.list-queue}
(apply list-queue-append queues)
.
[R7RS list-queue] {scheme.list-queue} procをqueueの各要素に適用して得られた結果を要素とする 新たなリストキューを返します。
[R7RS list-queue] {scheme.list-queue} queueの各要素を、それにprocを適用して得られた結果に 置き替えます。
[R7RS list-queue] {scheme.list-queue} procをqueueの各要素に適用します。結果は捨てられます。
Next: R7RS比較器, Previous: R7RSリストキュー, Up: R7RS large [Contents][Index]
scheme.ephemeron
- R7RS Ephemeronこのモジュールはephemeronと呼ばれる、 キーとそれに関連づけられたデータについて弱参照を保持する構造を提供します。 元々はsrfi-142で定義されました。
Gaucheはweak vectorとして弱参照を提供しています (Weakベクタ参照)が、単なるweak pointer (それに指されている オブジェクトの回収を許す) だけでは、mappingのようなキーと値を関連付ける データ構造で弱参照を実現するには不十分なことが知られています。
ephemeronはキーとデータへの参照を保持するレコードで、以下の性質を持ちます。
ephemeronを完全に実装するには、GCとの統合が必要になります。 今のところ、GaucheのephemeronはGCとは独立に実装されており、 以下の制限があります:
ephemeron-broken?
が呼ばれる必要があります。
いつ回収されるかについての規定は仕様にないので、Gaucheの現在の実装もsrfi-142の 仕様は満たしているのですが、実際に使うにあたっては上記の制限を意識する必要があるでしょう。 将来的にはGCと統合されたephemeronをサポートしたいと思っています。
キーあるいはデータが回収された後は (この状態のephemeronを “broken” と呼びます)、 キーやデータを取り出そうとしても意味のある値は得られません。 ephemeron eを使う正しい手順は次のパターンです:
(let ([k (ephemeron-key e)] [d (ephemeron-datum e)]) (if (ephemeron-broken? e) (... k and d are invalid ...) (... k and d are valid ...)))
つまり、値を取り出した後でephemeronがbrokenかどうかを調べます。
先にephemeron-broken?
を呼んでしまうと、その呼び出しから
値を取り出すまでの間にGCが走ってキーやデータが回収されてしまう可能性があります。
[R7RS ephemeron] {scheme.ephemeron} keyをdatumに関連づけるephemeronを作って返します。
[R7RS ephemeron]
{scheme.ephemeron}
objがephemeronであれば#t
を、そうでなければ#fを返します。
[R7RS ephemeron]
{scheme.ephemeron}
それぞれ、ephemeronのキーとデータを返します。
ephemeronが既にbrokenだと、何が返されるかはわかりません。
なので常に、これらの手続きで値を取り出した後で ephemeron-broken?
を呼んで値が有効かどうかチェックする必要があります。
詳しくは上のscheme.ephemeron
のエントリを参照してください。
[R7RS ephemeron]
{scheme.ephemeron}
ephemeronがbrokenであれば#t
を、そうでなければ#f
を返します。
brokenであるとは、キーや値が既に回収されてしまって取り出せないことを意味します。
詳しくはscheme.ephemeron
のエントリを参照してください。
[R7RS ephemeron] {scheme.ephemeron} この手続きはそれ自体は何もしませんが、 この手続きが戻るまでkeyへの参照が保持されることを保証します。
Next: R7RS正規表現, Previous: R7RS Ephemeron, Up: R7RS large [Contents][Index]
scheme.comparator
- R7RS比較器This module defines comparators and related procedures.
Originally called srfi-128
.
Gauche supports comparators fully compatible to
scheme.comparator
built-in.
See 基本的な比較器, for the following procedures
defined in this module.
comparator? comparator-ordered? comparator-hashable? make-comparator make-pair-comparator make-list-comparator make-vector-comparator make-eq-comparator make-eqv-comparator make-equal-comparator boolean-hash char-hash char-ci-hash string-hash string-ci-hash symbol-hash number-hash hash-bound hash-salt make-default-comparator default-hash comparator-register-default! comparator-type-test-predicate comparator-equality-predicate comparator-ordering-predicate comparator-hash-function comparator-test-type comparator-check-type comparator-hash =? <? >? <=? >=? comparator-if<=>
Next: R7RSマップ, Previous: R7RS比較器, Up: R7RS large [Contents][Index]
scheme.regex
- R7RS正規表現This module provides operations on Scheme Regular Expressions (SRE). Originally defined as srfi-115.
SRE is just an S-expression with the structure summarized below.
With the exception of or
, any syntax that takes multiple
<sre>
processes them in a sequence. In other words (foo
<sre> ...)
is equivalent to (foo (seq <sre> ...))
.
Note: SRE uses the symbol |
for alteration, but the vertical bar
character is used for symbol escape in Gauche (and R7RS), so you have to
write such symbol as |\||
. We recommend to use or
instead.
<sre> ::= | <string> ; A literal string match. | <cset-sre> ; A character set match. | (* <sre> ...) ; 0 or more matches. | (zero-or-more <sre> ...) | (+ <sre> ...) ; 1 or more matches. | (one-or-more <sre> ...) | (? <sre> ...) ; 0 or 1 matches. | (optional <sre> ...) | (= <n> <sre> ...) ; <n> matches. | (exactly <n> <sre> ...) | (>= <n> <sre> ...) ; <n> or more matches. | (at-least <n> <sre> ...) | (** <n> <m> <sre> ...) ; <n> to <m> matches. | (repeated <n> <m> <sre> ...) | (|\|| <sre> ...) ; Alternation. | (or <sre> ...) | (: <sre> ...) ; Sequence. | (seq <sre> ...) | ($ <sre> ...) ; Numbered submatch. | (submatch <sre> ...) | (-> <name> <sre> ...) ; Named submatch. <name> is | (submatch-named <name> <sre> ...) ; a symbol. | (w/case <sre> ...) ; Introduce a case-sensitive context. | (w/nocase <sre> ...) ; Introduce a case-insensitive context. | (w/unicode <sre> ...) ; Introduce a unicode context. | (w/ascii <sre> ...) ; Introduce an ascii context. | (w/nocapture <sre> ...) ; Ignore all enclosed submatches. | bos ; Beginning of string. | eos ; End of string. | bol ; Beginning of line. | eol ; End of line. | bow ; Beginning of word. | eow ; End of word. | nwb ; A non-word boundary. | (word <sre> ...) ; An SRE wrapped in word boundaries. | (word+ <cset-sre> ...) ; A single word restricted to a cset. | word ; A single word. | (?? <sre> ...) ; A non-greedy pattern, 0 or 1 match. | (non-greedy-optional <sre> ...) | (*? <sre> ...) ; Non-greedy 0 or more matches. | (non-greedy-zero-or-more <sre> ...) | (**? <m> <n> <sre> ...) ; Non-greedy <m> to <n> matches. | (non-greedy-repeated <sre> ...) | (atomic <sre> ...) ; Atomic clustering. | (look-ahead <sre> ...) ; Zero-width look-ahead assertion. | (look-behind <sre> ...) ; Zero-width look-behind assertion. | (neg-look-ahead <sre> ...) ; Zero-width negative look-ahead assertion. | (neg-look-behind <sre> ...) ; Zero-width negative look-behind assertion. | (backref <n-or-name>) ; Match a previous submatch.
The grammar for cset-sre
is as follows.
<cset-sre> ::= | <char> ; literal char | "<char>" ; string of one char | <char-set> ; embedded SRFI 14 char set | (<string>) ; literal char set | (char-set <string>) | (/ <range-spec> ...) ; ranges | (char-range <range-spec> ...) | (or <cset-sre> ...) ; union | (|\|| <cset-sre> ...) | (and <cset-sre> ...) ; intersection | (& <cset-sre> ...) | (- <cset-sre> ...) ; difference | (- <difference> ...) | (~ <cset-sre> ...) ; complement of union | (complement <cset-sre> ...) | (w/case <cset-sre>) ; case and unicode toggling | (w/nocase <cset-sre>) | (w/ascii <cset-sre>) | (w/unicode <cset-sre>) | any | nonl | ascii | lower-case | lower | upper-case | upper | title-case | title | alphabetic | alpha | alphanumeric | alphanum | alnum | numeric | num | punctuation | punct | symbol | graphic | graph | whitespace | white | space | printing | print | control | cntrl | hex-digit | xdigit <range-spec> ::= <string> | <char>
<string>
A literal string.
(regexp-search "needle" "hayneedlehay") ⇒ #<regexp-match> (regexp-search "needle" "haynEEdlehay") ⇒ #f
(seq <sre> ...)
(: <sre> ...)
A sequence of patterns that should be matched in the same order. This
is the same as RE syntax (?:re…)
(regexp-search '(: "one" space "two" space "three") "one two three") ⇒ #<regexp-match>
(or <sre> ...)
(|\|| <sre> ...)
Matches one of the given patterns. This is the same as
RE syntax pattern1|pattern2|…
(regexp-search '(or "eeney" "meeney" "miney") "meeney") ⇒ #<regexp-match> (regexp-search '(or "eeney" "meeney" "miney") "moe") ⇒ #f
(w/nocase <sre> ...)
Changes to match the given patterns case-insensitively. Sub-patterns
can still be made sensitive with w/case
. This is the same as
RE syntax (?i:re…)
(regexp-search "needle" "haynEEdlehay") ⇒ #f (regexp-search '(w/nocase "needle") "haynEEdlehay") ⇒ #<regexp-match> (regexp-search '(~ ("Aab")) "B") ⇒ #<regexp-match> (regexp-search '(~ ("Aab")) "b") ⇒ #f (regexp-search '(w/nocase (~ ("Aab"))) "B") ⇒ #f (regexp-search '(w/nocase (~ ("Aab"))) "b") ⇒ #f (regexp-search '(~ (w/nocase ("Aab"))) "B") ⇒ #f (regexp-search '(~ (w/nocase ("Aab"))) "b") ⇒ #f
(w/case <sre> ...)
Changes to match the given patterns case-sensitively. Sub-patterns can
still be made case-insensitive. This is the same as RE syntax
(?-i:re…)
. This is the default.
(regexp-search '(w/nocase "SMALL" (w/case "BIG")) "smallBIGsmall") ⇒ #<regexp-match> (regexp-search '(w/nocase (~ (w/case ("Aab")))) "b") ⇒ #f
(w/ascii <sre> ...)
Limits the character sets and other predefined patterns to ASCII. This
affects patterns or character sets like any
, alpha
,
(word)
…
(regexp-search '(w/ascii bos (* alpha) eos) "English") ⇒ #<regexp-match> (regexp-search '(w/ascii bos (* alpha) eos) "Ελληνική") ⇒ #f
(w/unicode <sre> ...)
Changes the character sets and other predefined patterns back to
Unicode if w/ascii
has been used in the outer scope. This is
the default.
(regexp-search '(w/unicode bos (* alpha) eos) "English") ⇒ #<regexp-match> (regexp-search '(w/unicode bos (* alpha) eos) "Ελληνική") ⇒ #<regexp-match>
(w/nocapture <sre> ...)
Disables capturing for all submatch
and submatch-named
inside.
(let ((number '($ (+ digit)))) (cdr (regexp-match->list (regexp-search `(: ,number "-" ,number "-" ,number) "555-867-5309"))) ; ⇒ '("555" "867" "5309") (cdr (regexp-match->list (regexp-search `(: ,number "-" (w/nocapture ,number) "-" ,number) "555-867-5309")))) ⇒ '("555" "5309")
(optional <sre> ...)
(? <sre> ...)
Matches the pattern(s) one or zero times.
(regexp-search '(: "match" (? "es") "!") "matches!") ⇒ #<regexp-match> (regexp-search '(: "match" (? "es") "!") "match!") ⇒ #<regexp-match> (regexp-search '(: "match" (? "es") "!") "matche!") ⇒ #f
(zero-or-more <sre> ...)
(* <sre> ...)
Matches the pattern(s) zero or more times.
(regexp-search '(: "<" (* (~ #\>)) ">") "<html>") ⇒ #<regexp-match> (regexp-search '(: "<" (* (~ #\>)) ">") "<>") ⇒ #<regexp-match> (regexp-search '(: "<" (* (~ #\>)) ">") "<html") ⇒ #f
(one-or-more <sre> ...)
(+ <sre> ...)
Matches the pattern(s) at least once.
(regexp-search '(: "<" (+ (~ #\>)) ">") "<html>") ⇒ #<regexp-match> (regexp-search '(: "<" (+ (~ #\>)) ">") "<a>") ⇒ #<regexp-match> (regexp-search '(: "<" (+ (~ #\>)) ">") "<>") ⇒ #f
(at-least n <sre> ...)
(>= n <sre> ...)
Matches the pattern(s) at least n
times.
(regexp-search '(: "<" (>= 3 (~ #\>)) ">") "<table>") ⇒ #<regexp-match> (regexp-search '(: "<" (>= 3 (~ #\>)) ">") "<pre>") ⇒ #<regexp-match> (regexp-search '(: "<" (>= 3 (~ #\>)) ">") "<tr>") ⇒ #f
(exactly n <sre> ...)
(= n <sre> ...)
Matches the pattern(s) exactly n
times.
(regexp-search '(: "<" (= 4 (~ #\>)) ">") "<html>") ⇒ #<regexp-match> (regexp-search '(: "<" (= 4 (~ #\>)) ">") "<table>") ⇒ #f
(repeated from to <sre> ...)
(** from to <sre> ...)
Matches the pattern(s) at least from
times and up to to
times.
(regexp-search '(: (= 3 (** 1 3 numeric) ".") (** 1 3 numeric)) "192.168.1.10") ⇒ #<regexp-match> (regexp-search '(: (= 3 (** 1 3 numeric) ".") (** 1 3 numeric)) "192.0168.1.10") ⇒ #f
(submatch <sre> ...)
($ <sre> ...)
Captures the matched string. Each capture is numbered increasing from one (capture zero is the entire matched string). For nested captures, the numbering scheme is depth-first walk.
(submatch-named <name> <sre> ...)
(-> <name> <sre> ...)
Captures the matched string and assigns a name to it in addition to a
number. This is the equivalent of (?<name>re…)
(backref <n-or-name>)
Matches a previously matched submatch. This is the same as
RE syntax \n
or \k<name>
.
<char>
A character set contains a single character.
(regexp-matches '(* #\-) "---") ⇒ #<regexp-match> (regexp-matches '(* #\-) "-_-") ⇒ #f
"<char>"
A character set contains a single character. This is technically ambiguous with SRE matching a literal string. However the end result of both syntaxes is the same.
<char-set>
A SRFI-14 character set.
Note that while currently there is no portable written representation
of SRFI 14 character sets, you can use Gauche reader syntax
#[char-set-spec]
, see 文字集合.
(regexp-partition `(+ ,char-set:vowels) "vowels") ⇒ ("v" "o" "w" "e" "ls")
(char-set <string>)
(<string>)
A character set contains the characters in the given string. This is
the same as `(char-set ,(string->char-set <string>))
.
(regexp-matches '(* ("aeiou")) "oui") ⇒ #<regexp-match> (regexp-matches '(* ("aeiou")) "ouais") ⇒ #f (regexp-matches '(* ("e\x0301")) "e\x0301") ⇒ #<regexp-match> (regexp-matches '("e\x0301") "e\x0301") ⇒ #f (regexp-matches '("e\x0301") "e") ⇒ #<regexp-match> (regexp-matches '("e\x0301") "\x0301") ⇒ #<regexp-match> (regexp-matches '("e\x0301") "\x00E9") ⇒ #f
(char-range <range-spec> ...)
(/ <range-spec> ...)
A character set contains the characters within
<range-set>
. This is the same as RE syntax []
.
(regexp-matches '(* (/ "AZ09")) "R2D2") ⇒ #<regexp-match> (regexp-matches '(* (/ "AZ09")) "C-3PO") ⇒ #f
(or <cset-sre> ...)
(|\|| <cset-sre> ...)
A shorthand for `(char-set ,(char-set-union <cset-sre>...))
.
(complement <cset-sre> ...)
(~ <cset-sre> ...)
A shorthand for `(char-set ,(char-set-complement <cset-sre>...))
.
(difference <cset-sre> ...)
(- <cset-sre> ...)
A shorthand for `(char-set ,(char-set-difference <cset-sre>...))
.
(regexp-matches '(* (- (/ "az") ("aeiou"))) "xyzzy") ⇒ #<regexp-match> (regexp-matches '(* (- (/ "az") ("aeiou"))) "vowels") ⇒ #f
(and <cset-sre> ...)
(& <cset-sre> ...)
A shorthand for `(char-set ,(char-set-intersection <cset-sre>...))
.
(regexp-matches '(* (& (/ "az") (~ ("aeiou")))) "xyzzy") ⇒ #<regexp-match> (regexp-matches '(* (& (/ "az") (~ ("aeiou")))) "vowels") ⇒ #f
(w/case <cset-sre>)
(w/nocase <cset-sre>)
(w/ascii <cset-sre>)
(w/unicode <cset-sre>)
This is similar to the SRE equivalent, listed to indicate that they can also be applied on character sets.
Note that if w/ascii
is in effect, these character sets will
return the ASCII subset. Otherwise they return full Unicode ones.
any
Matches any character. This is the .
in regular expression.
nonl
Matches any character other than #\return
or #\newline
.
ascii
A shorthand for `(char-set ,char-set:ascii)
.
lower-case
lower
A shorthand for `(char-set ,char-set:lower-case)
.
upper-case
upper
A shorthand for `(char-set ,char-set:upper-case)
.
title-case
title
A shorthand for `(char-set ,char-set:title-case)
.
alphabetic
alpha
A shorthand for `(char-set ,char-set:letter)
.
numeric
num
A shorthand for `(char-set ,char-set:digit)
.
alphanumeric
alphanum
alnum
A shorthand for `(char-set ,char-set:letter+digit)
.
punctuation
punct
A shorthand for `(char-set ,char-set:punctuation)
.
symbol
A shorthand for `(char-set ,char-set:symbol)
.
graphic
graph
A shorthand for `(char-set ,char-set:graphic)
.
(or alphanumeric punctuation symbol)
whitespace
white
space
A shorthand for `(char-set ,char-set:whitespace)
.
printing
print
A shorthand for `(char-set ,char-set:printing)
.
control
cntrl
A character set contains ASCII characters with from 0 to 31.
hex-digit
xdigit
A shorthand for `(char-set ,char-set:hex-digit)
.
bos
eos
Matches the beginning of the string. If start/end parameters are specified, matches the start or end of the substring as specified.
bol
eol
Matches the beginning or end of a line (or the string). For single
line matching, this is the same as bos
and eos
. A line
is interpreted the same way with read-line
.
bow
eow
Matches the beginning or the end of a word.
(regexp-search '(: bow "foo") "foo") ⇒ #<regexp-match> (regexp-search '(: bow "foo") "<foo>>") ⇒ #<regexp-match> (regexp-search '(: bow "foo") "snafoo") ⇒ #f (regexp-search '(: "foo" eow) "foo") ⇒ #<regexp-match> (regexp-search '(: "foo" eow) "foo!") ⇒ #<regexp-match> (regexp-search '(: "foo" eow) "foobar") ⇒ #f
nwb
A shorthand for (neg-look-ahead (or bow eow))
.
(word <sre> ...)
Matches the word boundary around the given SRE:
(: bow <sre> ... eow)
(word+ <cset-sre> ...)
Matches a single word composed of characters of the given characters sets:
(word (+ (and (or alphanumeric "_") (or cset-sre ...))))
word
A shorthand for (word+ any)
.
(non-greedy-optional <sre> ...)
(?? <sre> ...)
The non-greedy equivalent of (optional <sre>...)
. This is the
same as RE syntax re??
(non-greedy-zero-or-more< <sre> ...)
(*? <sre> ...)
The non-greedy equivalent of (zero-or-more <sre>...)
. This is
the same as RE syntax re*?
(non-greedy-repeated <m> <n> <sre> ...)
(**? <m> <n> <sre> ...)
The non-greedy equivalent of (repeated <sre>...)
. This is the
same as RE syntax re{n,m}?
(atomic <sre> ...)
Atomic clustering. Once <sre> ...
matches, the match is fixed;
even if the following pattern fails, the engine won’t backtrack to try
the alternative match in <sre> ...
. This is Gauche extension
and is the same as RE syntax (?>pattern)
(look-ahead <sre> ...)
Zero-width look-ahead assertion. Asserts the sequence matches from the
current position, without advancing the position. This is the same as
RE syntax (?=pattern)
(regexp-matches '(: "regular" (look-ahead " expression") " expression") "regular expression") ⇒ #<regexp-match> (regexp-matches '(: "regular" (look-ahead " ") "expression") "regular expression") ⇒ #f
(look-behind <sre> ...)
Zero-width look-behind assertion. Asserts the sequence matches behind
the current position, without advancing the position. It is an error
if the sequence does not have a fixed length. This is the same as RE
syntax (?<=pattern)
(neg-look-ahead <sre> ...)
Zero-width negative look-ahead assertion. This is the same as
RE syntax (?!pattern)
(neg-look-behind <sre> ...)
Zero-width negative look-behind assertion. This is the same as RE
syntax (?<!pattern)
[R7RS regex]
{scheme.regex}
Compiles the given Scheme Regular Expression into a <regexp>
object. If re is already a regexp object, the object is
returned as-is.
[R7RS regex]
{scheme.regex}
A macro shorthand for (regexp `(: sre ...))
.
[R7RS regex] {scheme.regex} Returns the SRE corresponding to the given given regexp object. Note that if the regexp object is not created from an SRE, it may contain features that cannot be expressed in SRE and cause an error.
[R7RS regex]
{scheme.regex}
Returns the SRE of the given character set. Currently this is not
optimized. If you convert any
to SRE for example, you may get
an SRE listing every single character.
[R7RS regex] {scheme.regex} Returns true iff obj can be safely passed to regexp.
[R7RS regex] {scheme.regex} Returns true iff obj is a regexp.
[R7RS regex]
{scheme.regex}
Returns an <regexp-match>
object if re successfully matches the
entire string str or optionally from start (inclusive) to
end (exclusive), or #f
is the match fails.
For convenience, end accepts #f
and interprets it as the
end of the string.
The regexp-match object will contain information needed to extract any submatches.
[R7RS regex]
{scheme.regex}
Similar to regexp-matches
but returns #t
instead of a
<regexp-match>
object.
[R7RS regex]
{scheme.regex}
Similar to regexp-matches
except that re only has to
match a substring in str instead.
[R7RS regex] {scheme.regex} Calls the procedure kons for every match found in str with following four arguments:
<regexp-match>
object.
If finish is given, it is called after all matches with the same
parameters as calling kons except that #f
is passed
instead of <regexp-match>
and the result is returned. Otherwise
the result of the last kons call is returned.
(regexp-fold 'word (lambda (i m str acc) (let ((s (regexp-match-submatch m 0))) (cond ((assoc s acc) => (lambda (x) (set-cdr! x (+ 1 (cdr x))) acc)) (else `((,s . 1) ,@acc))))) '() "to be or not to be") ⇒ '(("not" . 1) ("or" . 1) ("be" . 2) ("to" . 2))
[R7RS regex] {scheme.regex} Returns a list of matched string or an empty list if no matches.
(regexp-extract '(+ numeric) "192.168.0.1") ⇒ ("192" "168" "0" "1")
[R7RS regex]
{scheme.regex}
Returns a list of not matched substrings. This can be seen as the
opposite of regexp-extract
where the matched strings are
removed instead of returned.
(regexp-split '(+ space) " fee fi fo\tfum\n") ⇒ ("fee" "fi" "fo" "fum") (regexp-split '(",;") "a,,b,") ⇒ ("a" "" "b" "") (regexp-split '(* numeric) "abc123def456ghi789") ⇒ ("abc" "def" "ghi" "")
[R7RS regex]
{scheme.regex}
Returns a list of all matched and not matched substrings. In other
words it’s the combination of regexp-extract
and
regexp-split
where the boundary of matched strings are used to
split the original string.
(regexp-partition '(+ (or space punct)) "") ⇒ ("") (regexp-partition '(+ (or space punct)) "Hello, world!\n") ⇒ ("Hello" ", " "world" "!\n") (regexp-partition '(+ (or space punct)) "¿Dónde Estás?") ⇒ ("" "¿" "Dónde" " " "Estás" "?") (regexp-partition '(* numeric) "abc123def456ghi789") ⇒ ("abc" "123" "def" "456" "ghi" "789")
[R7RS regex] {scheme.regex} Returns a new string where the first matched substring is replaced with subst. If count is specified, the count-th match will be replaced instead of the first one.
subst can be either a string (the replacement), an integer or a symbol to refer to the capture group that will be used as the replacement, or a list of those.
The special symbols pre
and post
use the substring to
the left or right of the match as replacement, respectively.
subst could also be a procedure, which is called with the given match object and the result will be used as the replacement.
The optional parameters start and end essentially transform the substitution into this
(regexp-replace re (substring str start end) subst)
except that end can take #f
which is the same as
(string-length str)
.
(regexp-replace '(+ space) "one two three" "_") ⇒ "one_two three" (regexp-replace '(+ space) "one two three" "_" 1 10) ⇒ "ne_two th" (regexp-replace '(+ space) "one two three" "_" 0 #f 0) ⇒ "one_two three" (regexp-replace '(+ space) "one two three" "_" 0 #f 1) ⇒ "one two_three" (regexp-replace '(+ space) "one two three" "_" 0 #f 2) ⇒ "one two three"
Note that Gauche also has a builtin procedure of the same name, but works slightly differently, see 正規表現を使う.
[R7RS regex]
{scheme.regex}
Returns a new string where all matches in str are replaced with
subst. subst can also take a string, a number, a symbol or
a procedure similar to regexp-replace
.
(regexp-replace-all '(+ space) "one two three" "_") ⇒ "one_two_three"
Note that Gauche also has a builtin procedure of the same name, but works slightly differently, see 正規表現を使う.
[R7RS regex]
{scheme.regex}
Returns true iff obj is a <regexp-match>
object.
(regexp-match? (regexp-matches "x" "x")) ⇒ #t (regexp-match? (regexp-matches "x" "y")) ⇒ #f
[R7RS regex]
{scheme.regex}
Returns the number of matches in match except the implicit zero
full match. This is just an alias of rxmatch-num-matches
minus
one.
(regexp-match-count (regexp-matches "x" "x")) ⇒ 0 (regexp-match-count (regexp-matches '($ "x") "x")) ⇒ 1
[R7RS regex]
{scheme.regex}
This is an alias of rxmatch-substring
(regexp-match-submatch (regexp-search 'word "**foo**") 0) ⇒ "foo" (regexp-match-submatch (regexp-search '(: "*" ($ word) "*") "**foo**") 0) ⇒ "*foo*" (regexp-match-submatch (regexp-search '(: "*" ($ word) "*") "**foo**") 1) ⇒ "foo"
[R7RS regex]
{scheme.regex}
This is an alias of regexp-match-submatch-start
.
(regexp-match-submatch-start (regexp-search 'word "**foo**") 0) ⇒ 2 (regexp-match-submatch-start (regexp-search '(: "*" ($ word) "*") "**foo**") 0) ⇒ 1 (regexp-match-submatch-start (regexp-search '(: "*" ($ word) "*") "**foo**") 1) ⇒ 2
[R7RS regex]
{scheme.regex}
This is an alias of regexp-match-submatch-end
.
(regexp-match-submatch-end (regexp-search 'word "**foo**") 0) ⇒ 5 (regexp-match-submatch-end (regexp-search '(: "*" ($ word) "*") "**foo**") 0) ⇒ 6 (regexp-match-submatch-end (regexp-search '(: "*" ($ word) "*") "**foo**") 1) ⇒ 5
[R7RS regex]
{scheme.regex}
This is an alias of rxmatch-substrings
(regexp-match->list (regexp-search '(: ($ word) (+ (or space punct)) ($ word)) "cats & dogs")) ⇒ '("cats & dogs" "cats" "dogs")
Next: R7RS整数除算, Previous: R7RS正規表現, Up: R7RS large [Contents][Index]
scheme.mapping
- R7RSマップThis module defines immutable mappings from keys to values.
Originally called srfi-146
and srfi-146.hash
.
The scheme.mapping
module provides mapping objects,
where keys have total order. The scheme.mapping.hash
module
provides hashmap
objects, where keys can be hashed.
Currently, Gauche uses built-in <tree-map>
for the mapping object,
and built-in <hash-table>
for the hashmap object.
The actual implementation
may be changed in future versions, so the user must not rely on the
underlying implementations.
The caller must treat mappings and hashmaps as immutable object.
The modules also provide “linear update” APIs, which is allowed
to mutate the mappings passed to the arguments, under assumption
that the argument won’t be used afterwards. The linear update APIs
are marked with !
at the end of the name.
• Mappings: | ||
• Hashmaps: |
{scheme.mapping}
On Gauche, this is just an alias of <tree-map>
.
[R7RS mapping] {scheme.mapping} Creates a new mapping with the given comparator, whose initial content is provided by key value ….
The comparator argument must be a comparator (see 基本的な比較器).
The key value … arguments must be even length, alternating keys and values.
(define m (mapping default-comparator 'a 1 'b 2)) (mapping-ref m 'a) ⇒ 1 (mapping-ref m 'b) ⇒ 2
[R7RS mapping] {scheme.mapping} Creates a new mapping, whose content is populated by three procedures, p, f and g, and a seed value seed, as follows.
In each iteration, we have a current seed value, whose initial value is seed.
First, p, a stop predicate, is applied to the current seed value. If it returns true, we stop iteration and returns the new mapping.
Next, f is applied to the current seed value. It must return two values. The first one is for a key and the second one for the value. We add this pair to the mapping.
Then, g is applied to the current seed value. The result becomes the seed value of the next iteration. And we iterate.
The following example creates a mapping that maps ASCII characters to their character codes:
(mapping-unfold (cut >= <> 128) (^c (values (integer->char c) c)) (cut + <> 1) 0 default-comparator)
[R7RS mapping]
{scheme.mapping}
Similar to mapping
, but keys are given in the ascending order
w.r.t. the comparator. An implementation may use more efficient algorithm
than mapping
. In Gauche, this is the same as mapping
at this moment.
[R7RS mapping]
{scheme.mapping}
Similar to mapping-unfold
, but keys are generated in the
ascending order
w.r.t. the comparator. An implementation may use more efficient algorithm
than mapping-unfold
.
In Gauche, this is the same as mapping-unfold
at this moment.
[R7RS mapping]
{scheme.mapping}
Returns #t
iff obj is a mapping object.
[R7RS mapping]
{scheme.mapping}
M must be a mapping.
Returns #t
if m is empty, #f
otherwise.
In Gauche, this is same as tree-map-empty?
(see ツリーマップ).
[R7RS mapping]
{scheme.mapping}
M must be a mapping.
Returns #t
if m has an entry with key,
#f
otherwise.
In Gauche, this is same as tree-map-exists?
(see ツリーマップ).
[R7RS mapping]
{scheme.mapping}
Returns #t
iff two mappings m1 and m2 have no keys
in common. In other words, there’s no such key K that
satisfy both (mapping-contains? m1 K)
and
(mapping-contains? m2 K)
.
[R7RS mapping] {scheme.mapping} Get the value from a mapping m associated with key, and calls success on the value, and returns its result. If m doesn’t have key, failure is invoked with no arguments and its result is returned. Both success and failure is called in tail context.
When failure is omitted and key is not found, an error
is signaled. When success is omitted, identity
is
assumed.
[R7RS mapping] {scheme.mapping} Returns the value associated to key from a mapping m. If m doesn’t have key, default is returned.
[R7RS mapping] {scheme.mapping} Returns a comparator used to compare keys in a mapping m. See 基本的な比較器, for the details of comparators.
Note that the basic premise of mappings srfi is to treat mappings as immutable. Each updating operation comes with a purely functional version (without bang) and a linear update version (with bang), but the linear update version may not require to destructively modiy the passed mapping; it’s merely a hint that it may reuse the argument for the efficiency. You always need to use the returned mapping as the result of update. If you use linear update versions, you shouldn’t use the passed mapping afterwards, for there’s no guarantee how the state of the passed mapping is.
[R7RS mapping]
{scheme.mapping}
The arg … are alternating between key and value. Returns
a mapping that contains all the entries in m plus given
keys and values, with the same comparator as m.
Linear update version mapping-adjoin!
may destructively modify
m to create the return value, while mapping-adjoin
creates a new mapping.
Arguments are processed in order. If there’s already an entry in m with the same key as given to arg, the original entry remains.
(mapping-adjoin (mapping default-comparator 'a 1 'b 2) 'c 3 'a 4 'c 5)
⇒ mapping with a → 1, b → 2, c → 3
[R7RS mapping]
{scheme.mapping}
The arg … are alternating between key and value. Returns
a mapping that contains all the entries in m plus given
keys and values, with the same comparator as m.
Linear update version mapping-set!
may destructively modify
m to create the return value, while mapping-set
creates a new mapping.
Arguments are processed in order. If there’s already an entry in m with the same key as given to arg, the new key-value pair supersedes the old one.
(mapping-set (mapping default-comparator 'a 1 'b 2) 'c 3 'a 4 'c 5)
⇒ mapping with a → 4, b → 2, c → 5
[R7RS mapping] {scheme.mapping} If the mapping m has an entry of key, return a mapping with the value of the entry replaced for value. If m doesn’t have an entry with key, m is returned unchanged.
Linear update version mapping-replace!
may destructively modify
m to produce the return value, while mapping-replace
creates a new mapping.
(mapping-replace (mapping default-comparator 'a 1 'b 2) 'a 3) ⇒ mapping with a → 3, b → 2 (mapping-replace (mapping default-comparator 'a 1 'b 2) 'c 3) ⇒ mapping with a → 1, b → 2
[R7RS mapping] {scheme.mapping} Returns a mapping that is the same as m except its entries with any of the given key … being removed. Keys that are not in m are ignored.
Linear update version mapping-delete!
may destructively modify
m to produce the return value, while mapping-delete
creates a new mapping.
[R7RS mapping] {scheme.mapping} Returns a mapping that is the same as m except its entries with any of the given keys in key-list being removed. Keys that are not in m are ignored.
Linear update version mapping-delete-all!
may destructively modify
m to produce the return value, while mapping-delete-all
creates a new mapping.
[R7RS mapping] {scheme.mapping} Looks up key in the mapping m, and returns two values, m and the associated value. If m does not contain an entry with key, a thunk make-value is invoked, and creates a new mapping that contains all entries in m plus a new entry with key and the return value of make-value, then returns the new mapping and the return value of make-value.
Linear update version mapping-intern!
may destructively modify
m to produce the return value, while mapping-intern
creates a new mapping.
(mapping-intern (mapping default-comparator 'a 1) 'b (^[] 2)) ⇒ mapping with a → 1, b → 2 and 2 (mapping-intern (mapping default-comparator 'a 1) 'a (^[] 2)) ⇒ mapping with a → 1 and 1
[R7RS mapping] {scheme.mapping} Semantically equivalent to this:
(mapping-set m var (updater (mapping-ref m key failure success)))
The failure and success optional arguments are procedures
with zero and one arguments, respectively. When omitted,
failure defaults to a thunk that raises an error,
and success defaults to identity
.
First, key is looked up in m. If an entry is found, the associated value is passed to success; otherwise, failure is called with no arguments. Either way, let the returned value be v0.
Then, v0 is passed to updater. Let its result be v1.
Finally, a mapping with the same entries as m except the value of key is altered to v1 is returned.
Linear update version mapping-update!
may destructively modify
m to produce the return value, while mapping-update
creates a new mapping.
(mapping-update (mapping default-comparator 'a 1) 'a (pa$ + 1)) ⇒ mapping with a → 2 (mapping-update (mapping default-comparator) 'a (pa$ + 1) (^[] 0)) ⇒ mapping with a → 1
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping}
[R7RS mapping] {scheme.mapping.hash} Creates a new hashmap with the given comparator, whose initial content is provided by key value ….
The comparator argument must be a comparator (see 基本的な比較器).
The key value … arguments must be even length, alternating keys and values.
(define m (hashmap default-comparator 'a 1 'b 2)) (hashmap-ref m 'a) ⇒ 1 (hashmap-ref m 'b) ⇒ 2
[R7RS mapping] {scheme.mapping.hash} Creates a new hashmap, whose content is populated by three procedures, p, f and g, and a seed value seed, as follows.
In each iteration, we have a current seed value, whose initial value is seed.
First, p, a stop predicate, is applied to the current seed value. If it returns true, we stop iteration and returns the new hashmap.
Next, f is applied to the current seed value. It must return two values. The first one is for a key and the second one for the value. We add this pair to the hashmap.
Then, g is applied to the current seed value. The result becomes the seed value of the next iteration. And we iterate.
The following example creates a hashmap that maps ASCII characters to their character codes:
(hashmap-unfold (cut >= <> 128) (^c (values (integer->char c) c)) (cut + <> 1) 0 default-comparator)
[R7RS mapping]
{scheme.mapping.hash}
Returns #t
iff obj is a hashmap object.
[R7RS mapping]
{scheme.mapping.hash}
M must be a hashmap.
Returns #t
if m is empty, #f
otherwise.
In Gauche, this is same as tree-map-empty?
(see ツリーマップ).
[R7RS mapping]
{scheme.mapping.hash}
M must be a hashmap.
Returns #t
if m has an entry with key,
#f
otherwise.
In Gauche, this is same as tree-map-exists?
(see ツリーマップ).
[R7RS mapping]
{scheme.mapping.hash}
Returns #t
iff two hashmaps m1 and m2 have no keys
in common. In other words, there’s no such key K that
satisfy both (hashmap-contains? m1 K)
and
(hashmap-contains? m2 K)
.
[R7RS mapping] {scheme.mapping.hash} Get the value from a hashmap m associated with key, and calls success on the value, and returns its result. If m doesn’t have key, failure is invoked with no arguments and its result is returned. Both success and failure is called in tail context.
When failure is omitted and key is not found, an error
is signaled. When success is omitted, identity
is
assumed.
[R7RS mapping] {scheme.mapping.hash} Returns the value associated to key from a hashmap m. If m doesn’t have key, default is returned.
[R7RS mapping] {scheme.mapping.hash} Returns a comparator used to compare keys in a hashmap m. See 基本的な比較器, for the details of comparators.
Note that the basic premise of hashmaps srfi is to treat hashmaps as immutable. Each updating operation comes with a purely functional version (without bang) and a linear update version (with bang), but the linear update version may not require to destructively modiy the passed hashmap; it’s merely a hint that it may reuse the argument for the efficiency. You always need to use the returned hashmap as the result of update. If you use linear update versions, you shouldn’t use the passed hashmap afterwards, for there’s no guarantee how the state of the passed hashmap is.
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
[R7RS mapping] {scheme.mapping.hash}
Next: R7RSビット演算, Previous: R7RSマップ, Up: R7RS large [Contents][Index]
scheme.division
- R7RS整数除算このモジュールは、様々な整数除算操作を包括的に提供します。
整数除算の商と剰余には、除数と被除数の符号の考慮によって、
いくつかの定義が考えられます。
Gaucheはそのうちいくつかの定義を組み込みで提供しています:
R5RSのquotient
、remainder
、modulo
、
R6RSのdiv
、mod
、div0
、mod0
、
そしてR7RSの
floor-quotient
、floor-remainder
、floor/
、
truncate-quotient
、truncate-remainder
、truncate/
、です。
このモジュールは、R7RSの手続きにさらに
ceiling
、
round
、euclidean
、balanced
のバリエーションを加えます。
scheme.divisionの以下の手続きについては組み込みになっているので、 数値の演算を参照してください。
floor-quotient floor-remainder floor/ truncate-quotient truncate-remainder truncate/
[R7RS division] {scheme.division}
ceiling-quotient = ceiling(n / d) ceiling-remainder = n - d * ceiling-quotient ceiling/ = values(ceiling-quotient, ceiling-remainder)
[R7RS division] {scheme.division}
round-quotient = round(n/d) round-remainder = n - d * round-quotient round/ = values(round-quotient, round-remainder)
[R7RS division] {scheme.division}
euclidean-quotient = floor(n / d) if d > 0 ceiling(n / d) if d < 0 euclidean-remainder = n - d * euclidean-quotient euclidean/ = values(euclidean-quotient, euclidean-remainder)
Eclideanバリエーションは、0 <= remainder < abs(d)
という関係を常に満たします。
この定義はR6RSのdiv
、mod
およびdiv-and-mod
と同じです。
但し、R6RS版は非整数を引数に取ることもできます(数値の演算参照)。
[R7RS division] {scheme.division}
balanced-quotient = roundup(n / d) balanced-remainder = n - d * balanced-quotient balanced/ = values(balanced-quotient, balanced-remainder) ここでroundup(x)は、|x| - floor(|x|) < 0.5 ならゼロの方向に、 |x| - floor(|x|) >= 0.5 ならゼロと逆方向丸めます
Balancedバージョンは-abs(d/2) <= remainder < abs(d/2)
という
関係を常に満たします。これはR6RSの
div0
、mod0
およびdiv0-and-mod0
と同じです。
但し、R6RS版は非整数を引数に取ることもできます(数値の演算参照)。
Next: R7RS fixnum, Previous: R7RS整数除算, Up: R7RS large [Contents][Index]
scheme.bitwise
- R7RSビット演算このモジュールは包括的なビット演算手続きを提供します。 元はsrfi-151で、 ほぼsrfi-60のスーパーセットですが、いくつかの手続きは一貫性と互換性のために 名前が変わりました(srfi-60については整数に対するビット操作参照)。 srfi-60も以前のコードとの互換性のため残されますが、新たに書くコードは このモジュールを使うことを推奨します。
以下の手続きはGauche組み込みになっています。 説明は基本的なビット演算を参照してください。
integer-length copy-bit bit-field
[R7RS bitwise]
{scheme.bitwise}
nの各ビットを反転したものを返します。
組み込みのlognot
と同じです。(基本的なビット演算参照)。
[R7RS bitwise]
{scheme.bitwise}
引数が与えられない場合、これらはそれぞれ-1
、0
、0
、-1
を
返します。引数がひとつの場合はそれをそのまま返します。
引数が二つの場合は、それらのビット毎のand、ior、xor、及びeqv (xorの論理反転) を
取ったものを返します。3引数以上は、2引数の計算から導かれます。
(bitwise-xor a b c) ≡ (bitwise-xor a (bitwise-xor b c)) ≡ (bitwise-xor (bitwise-xor a b) c)
この定義では、3引数以上のbitwise-eqv
は
「すべての引数の該当ビットが同じである時に1」とはならないことに注意してください。
最初の3つの手続きはそれぞれ組み込みの
logand
、logior
、logxor
と同じです
(基本的なビット演算参照)。
[R7RS bitwise] {scheme.bitwise} これらの手続きは2引数固定です。
nand n0 n1 ≡ (NOT (AND n0 n1)) nor n0 n1 ≡ (NOT (OR n0 n1)) andc1 n0 n1 ≡ (AND (NOT n0) n1) andc2 n0 n1 ≡ (AND n0 (NOT n1)) orc1 n0 n1 ≡ (OR (NOT n0) n1) orc2 n0 n1 ≡ (OR n0 (NOT n1))
[R7RS bitwise]
{scheme.bitwise}
整数nをcountビット左シフトします。countが負ならば、
n
は-countビット右にシフトされることになります。
これは組み込みのash
と同じです(基本的なビット演算参照)。
[R7RS bitwise]
{scheme.bitwise}
nが正の場合はn中の1
であるビットの数を、
nが負の場合はn中の0
であるビットの数を返します。
組み込みのlogcount
と同じです(基本的なビット演算参照)。
[R7RS bitwise]
{scheme.bitwise}
整数を返します。戻り値のn番目のビットは、maskのn番目のビットが
1であればn0のn
番目のビット、0であればn1のn
番目のビット
になります。
(bitwise-if #b10101100 #b00110101 #b11001010) ⇒ #b01100110
[R7RS bitwise]
{scheme.bitwise}
nのLSBから数えてindex番目(0ベース)のビットが1
であれば
#t
を、0
であれば#f
を返します。
組み込みのlogbit?
と同じです(基本的なビット演算参照)。
[R7RS bitwise] {scheme.bitwise} nのindex1番目のビットとindex2番目のビットを入れ替えた整数を 返します。インデックスは0ベースでLSBから数えます。
[R7RS bitwise]
{scheme.bitwise}
n中で、maskでビットの立っている箇所のビットのいずれか(any-bit-set?
)
あるいはすべて(every-bit-set?
)が1
であれば#t
を返し、
それ以外では#f
を返します。
any-bit-set?
は組み込みのlogtest
とほぼ同じですが、
logtest
は可変長引数です
(基本的なビット演算参照)。
[R7RS bitwise]
{scheme.bitwise}
nを割り切る(expt 2 k)
のうち最大のkを返します。別の言い方をすれば、
nのビット列のうち一番右にある1
のインデックスを返します。
first-set-bit
の名前はそこからきています。
(first-set-bit 0) ⇒ -1 ; edge case
(first-set-bit 1) ⇒ 0
(first-set-bit 2) ⇒ 1
(first-set-bit 15) ⇒ 0
(first-set-bit 16) ⇒ 4
これはGauche組み込みのtwos-exponent-factor
と同じです
(基本的なビット演算参照)。
[R7RS bitwise]
{scheme.bitwise}
n中のstartビット目(含む)からendビット目(含まない)までの
ビットフィールドのうち、一つでもビットが立っていた場合(bit-field-any?
)、
あるいは全てのビットが立っていた場合(bit-field-every?
)に#t
を、
それ以外に#f
を返します。
[R7RS bitwise]
{scheme.bitwise}
n中のstartビット目(含む)からendビット目(含まない)までの
ビットを全て0
あるいは1
にした整数値を返します。
[R7RS bitwise] {scheme.bitwise} dst中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを、src中の下位(end-start)ビットで置き換えた 値を返します。
(bit-field-replace #b101010 #b010 1 4) ⇒ #b100100
組み込みのcopy-bit-field
と同じです(基本的なビット演算参照)。
[R7RS bitwise] {scheme.bitwise} dst中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを、 src中の同じ位置にあるビットフィールドに置き換えた値を返します。
(bit-field-replace-same #b111111 #b100100 1 4) ⇒ #b110101
[R7RS bitwise] {scheme.bitwise} n中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを、countビットだけ左にローテートした値を返します。 countが負の場合は-countビットだけ右にローテートします。
(bit-field-rotate #b110100100010000 -1 5 9) ⇒ 26768 ;#b110100010010000 (bit-field-rotate #b110100100010000 1 5 9) ⇒ 26672 ;#b110100000110000
[R7RS bitwise] {scheme.bitwise} n中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを逆順にしたものを返します。
(bit-field-reverse #b10100111 0 8) ⇒ 229 ; #b11100101
[R7RS bitwise]
{scheme.bitwise}
非負整数nを、長さlenの真偽値からなるリストまたはベクタにして返します。
ビットはLSBから順に取り出されます。lenが省略された場合は
(integer-length n)
が使われます。
(bits->vector #b101101110) ⇒ #(#f #t #t #t #f #t #t #f #t)
註: srfi-60にはinteger->list
という似た手続きがありますが、
ビットの順番が逆です。
[R7RS bitwise] {scheme.bitwise} 真偽値のリストまたはベクタを受け取り、 LSBから対応するビットを詰めた正確な整数を返します。 返り値は負になることはありません。
(list->bits '(#f #t #t #t #f #t #t #f #t)) ⇒ #b101101110
註: srfi-60にはlist->integer
という似た手続きがありますが、
ビットの順番が逆です。
[R7RS bitwise] {scheme.bitwise} 真偽値boolの列をLSBから順にビットとして詰めた整数を返します。 返り値は負になることはありません。
(bits #f #t #t #t #f #t #t #f #t) ⇒ #b101101110
註: srfi-60にはbooleans->integer
という似た手続きがありますが、
ビットの順番が逆です。
[R7RS bitwise]
{scheme.bitwise}
整数nのビットを、LSBから(integer-length n)
ビット分、順に調べ、
各ビットを真偽値とみなした値とシード値にkonsを適用してゆきます。
konsの戻り値が次のシード値となり、最後のkonsの結果が返されます。
(bitwise-fold cons '() #b10110111) ⇒ (#t #f #t #t #f #t #t #t)
[R7RS bitwise]
{scheme.bitwise}
整数nの各ビットを真偽値とみなした値に対して、
LSBから順に(integer-length n)
ビット分、procを適用してゆきます。
procの結果は捨てられます。
[R7RS bitwise]
{scheme.bitwise}
非負整数をLSBから順に1ビットづつ生成します。
seedは状態の値の初期値を与えます。
各繰り返しにおいて、pが現在の状態の値に適用され、
その結果が真ならば繰り返しは終了しbitwise-unfold
は蓄積されたビットを整数として
返します。
そうでなければ、fが現在の状態の値に適用され、
その結果が真の値なら対応するビットが1に、偽なら0になります。
次いでgが現在の状態の値に適用され、その結果が次の繰り返しにおける状態の値となります。
次の式は、nビット目が、nが素数なら1になっているような100ビット長の整数を返します。
(use math.prime) (bitwise-unfold (cut = 100 <>) small-prime? (cut + 1 <>) 0)
[R7RS bitwise] {scheme.bitwise} 整数nの各ビットをLSBから順に真偽値として生成するようなジェネレータを作って返します。 返されるジェネレータは無限です。
これはgauche.integer
のbits->generator
と似ていますが、
bits->generator
が作るジェネレータの方はnのinteger-length
で止まります。
詳しくはジェネレータを参照してください。
Next: R7RS flonum, Previous: R7RSビット演算, Up: R7RS large [Contents][Index]
scheme.fixnum
- R7RS fixnumこのモジュールはfixnumに特化した操作を提供します。 srfi-143として制定されました。
fixnumは絶対値が小さめの正確な整数で、極めて効率的に処理できます。 Gaucheのfixnumは、64ビット環境では62ビット、32ビット環境では30ビットの幅を持ちます。
このモジュールの手続きはfixnumの範囲でしか動作を定義されていませんが、 それを強制するのではないことに注意してください。fixnumでない引数を 渡したり、結果がfixnumの範囲外になった場合に何が起きるかは 処理系依存とされています。これらの手続きは、プログラマがその意図を コンパイラに伝えて最適化を期待するためのものと考えると良いでしょう。
現在のGaucheのアーキテクチャでは一般的な数値計算が効率よく実行できるようになっているので、 これらの手続きは対応する手続きの単なる別名になっています。 将来は、fixnum特有の最適化を導入するかもしれません。
手続きfixnum?
は組み込みなのでここでは説明しません。
数値に関する述語を参照してください。
[R7RS fixnum]
{scheme.fixnum}
2^(w-1) - 1
から-2^(w-1)
までの正確な整数が全てfixnumであるような
最大の正整数w
に束縛された変数です。
組み込み手続きfixnum-width
が返す値と同じです。
(数値の演算参照)。
Gaucheでは、これは通常32ビット環境で30
、64ビット環境で62
です。
[R7RS fixnum]
{scheme.fixnum}
最大および最小のfixnumに束縛された変数です。これらの値は
組み込み手続きgreatest-fixnum
およびleast-fixnum
が
返すものと同じです(数値の演算参照)。
次の表はGaucheでの典型的な値を示します。
Platform | fx-greatest | fx-least |
---|---|---|
32bit | 536,870,911 | -536,870,912 |
64bit | 2,305,843,009,213,693,951 | -2,305,843,009,213,693,952 |
[R7RS fixnum]
{scheme.fixnum}
これらは、fixnumのみに適用しなければならないことを除き、組み込みの
=
、<
、<=
、>
、>=
と同じです。
[R7RS fixnum]
{scheme.fixnum}
これらは、fixnumのみに適用しなければならないことを除き、組み込みの
zero?
、positive?
、negative?
、odd?
、even?
と同じです。
[R7RS fixnum]
{scheme.fixnum}
これらは、fixnumのみに適用しなければならないことを除き、組み込みの
max
およびmin
と同じです。
[R7RS fixnum]
{scheme.fixnum}
これらは、常に2つの引数を取ること、
fixnumのみに適用しなければならないこと、
そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、
組み込みの+
、-
、*
と同じです。
[R7RS fixnum]
{scheme.fixnum}
これは、fixnumのみに適用しなければならないこと、
そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、
組み込みの単項-
と同じです。
[R7RS fixnum]
{scheme.fixnum}
これらは、fixnumのみに適用しなければならないこと、
そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、
組み込みのquotient
、remainder
、abs
、square
と
同じです。
[R7RS fixnum]
{scheme.fixnum}
これらは、fixnumのみに適用しなければならないことを除き、
exact-integer-sqrt
と同じです。sqrt
ではありません。
数値の演算参照。
[R7RS fixnum]
{scheme.fixnum}
これらはそれぞれ、まず
(+ i j k)
、(- i j k)
、(+ (* i j) k)
を計算し、
その結果をfixnumの範囲内に収まる剰余R
と、溢れQ
に分割して
2つの値として返します。
すなわち、(+ (* Q (expt 2 fx-width)) R)
が上記計算と一致します。
Q
とR
は常にfixnumの範囲内であり、またR
は
- 2^(w-1) <= R < 2^(w-1)
を満たします。ただしw
は
fx-width
です。
(fx*/carry 1845917459 19475917581 4735374)
⇒ -942551854601421179 and 8
(+ (* 8 (expt 2 fx-width)) -942551854601421179)
⇒ 35950936292817682053
(+ (* 1845917459 19475917581) 4735374)
⇒ 35950936292817682053
これらは拡張精度整数演算をfixnum手続き上に効率よく実装するためのプリミティブです。 ただ、Gaucheでは必要ならbignumを使えば良いので、これらは単に互換性のために提供しています。
[R7RS fixnum]
{scheme.fixnum}
これらはfixnumのみに適用しなければならないことを除き、
組み込みのlognot
、 logand
、
logior
、 logxor
、 ash
、 integer-length
、
logcount
、
copy-bit
、 logbit?
、 bit-field
、
twos-exponent-factor
とそれぞれ同じです。
詳しくは基本的なビット演算参照。
[R7RS fixnum]
{scheme.fixnum}
これらはfixnumのみに適用しなければならないことを除き、
srfi-60のbitwise-if
、rotate-bit-field
、
reverse-bit-field
と同じです。
整数に対するビット操作参照。
Next: R7RS bytevectors, Previous: R7RS fixnum, Up: R7RS large [Contents][Index]
scheme.flonum
- R7RS flonumこのモジュールはflonumに特化した操作を提供します。 srfi-144として制定されました。
Gaucheでは、flonumはIEEE754倍精度浮動小数点数です。
このモジュールの手続きはflonumの範囲でしか動作を定義されていませんが、 それを強制するのではないことに注意してください。flonumでない引数を 渡した場合の動作は未定義です。これらの手続きは、プログラマがその意図を コンパイラに伝えて最適化を期待するためのものと考えると良いでしょう。
現在のGaucheのアーキテクチャでは一般的な数値計算が効率よく実行できるようになっているので、 これらの手続きは対応する手続きの単なる別名になっています。 将来は、flonum特有の最適化を導入するかもしれません。
手続きflonum?
は組み込みなのでここでは説明しません。
数値に関する述語を参照してください。
We also have a few constants in math.const
module
(see 定数).
[R7RS flonum] {scheme.flonum} e.
[R7RS flonum]
{scheme.flonum}
(/ e)
.
[R7RS flonum]
{scheme.flonum}
(square e)
.
[R7RS flonum]
{scheme.flonum}
(expt e (/ pi 4))
.
[R7RS flonum]
{scheme.flonum}
(log2 e)
. (Same as (/ (log 2))
).
[R7RS flonum]
{scheme.flonum}
(log10 e)
. (Same as (/ (log 10))
).
[R7RS flonum]
{scheme.flonum}
(log 2)
.
[R7RS flonum]
{scheme.flonum}
(/ (log 2))
. (Same as (log2 e)
).
[R7RS flonum]
{scheme.flonum}
(log 3)
.
[R7RS flonum]
{scheme.flonum}
(log pi)
.
[R7RS flonum]
{scheme.flonum}
(log 10)
.
[R7RS flonum]
{scheme.flonum}
(/ (log 10))
. (Same as (log10 e)
).
[R7RS flonum]
{scheme.flonum}
pi
.
[R7RS flonum]
{scheme.flonum}
(/ pi)
.
[R7RS flonum]
{scheme.flonum}
(* 2 pi)
.
[R7RS flonum]
{scheme.flonum}
(/ pi 2)
.
[R7RS flonum]
{scheme.flonum}
(/ pi 4)
.
[R7RS flonum]
{scheme.flonum}
(square pi)
.
[R7RS flonum]
{scheme.flonum}
(/ pi 180)
.
[R7RS flonum]
{scheme.flonum}
(/ 2 pi)
.
[R7RS flonum]
{scheme.flonum}
(/ 2 (sqrt pi))
.
(sqrt 2)
.
[R7RS flonum]
{scheme.flonum}
(sqrt 3)
.
[R7RS flonum]
{scheme.flonum}
(sqrt 5)
.
[R7RS flonum]
{scheme.flonum}
(sqrt 10)
.
[R7RS flonum]
{scheme.flonum}
(/ (sqrt 2))
.
[R7RS flonum]
{scheme.flonum}
(expt 2 1/3)
.
[R7RS flonum]
{scheme.flonum}
(expt 3 1/3)
.
[R7RS flonum]
{scheme.flonum}
(expt 2 1/4)
.
[R7RS flonum]
{scheme.flonum}
(/ (+ 1 (sqrt 5)) 2)
.
[R7RS flonum]
{scheme.flonum}
(log fl-phi)
.
[R7RS flonum]
{scheme.flonum}
(/ (log fl-phi))
.
[R7RS flonum] {scheme.flonum} Euler’s constant.
[R7RS flonum]
{scheme.flonum}
(exp fl-euler)
[R7RS flonum]
{scheme.flonum}
(sin 1)
[R7RS flonum]
{scheme.flonum}
(cos 1)
[R7RS flonum]
{scheme.flonum}
(gamma 1/2)
[R7RS flonum]
{scheme.flonum}
(gamma 1/3)
[R7RS flonum]
{scheme.flonum}
(gamma 2/3)
[R7RS flonum]
{scheme.flonum}
(gamma 1/3)
[R7RS flonum] {scheme.flonum} Bound to the largest/smallest positive finite flonum.
The latter is the same as (flonum-min-denormalized)
in Gauche
(see 数値の比較).
[R7RS flonum]
{scheme.flonum}
Gaucheの(flonum-epsilon)
と同じです。
(数値の比較参照)。
[R7RS flonum]
{scheme.flonum}
これが#t
を返した場合、(fl+* x y z)
は
(fl+ (fl* x y) z)
と同等の速度、もしくはより速く実行されます。
[R7RS flonum]
{scheme.flonum}
これらは、flinteger-exponent
が特別な場合に返し得る正確な整数値です。
値そのものに意味はなく、flinteger-exponent
の結果と比較するために使われます。
flinteger-exponent
は引数が0の時fl-integer-exponent-zero
を、
引数が+nan.0
の時fl-integer-exponent-nan
を返します。
[R7RS flonum] {scheme.flonum} numberと等しいflonum、もしくは等しい値がflonumで表現可能でなければもっとも近い flonumを返します。
numberが実数でなければ+nan.0
が返されます
(これはsrfi-144で推奨されている動作ですが、処理系によってはエラーを投げても良いことに
なっているので、ポータブルなコードを書くときは注意してください。)
[R7RS flonum] {scheme.flonum} flonum xからyの方向に1最小単位だけ進めたflonumを返します。 x = yなら、xそのものが返されます。
(fladjacent 1.0 2.0) ⇒ 1.0000000000000002 (fladjacent 1.0 0.0) ⇒ 0.9999999999999999
[R7RS flonum]
{scheme.flonum}
絶対値が(abs x)
と同じで、符号がyと同じであるようなflonumを返します。
[R7RS flonum]
{scheme.flonum}
(* x (expt 2 n))
なるflonumを返します。
Gaucheのldexp
と同じです (数値の変換参照)。
[R7RS flonum]
{scheme.flonum}
flonum xの整数部と小数部をそれぞれflonumとして返します。
Gaucheのmodf
と同じです (数値の変換参照)。
xが+inf.0
なら、+inf.0
と0.0
が、
xが-inf.0
なら、-inf.0
と-0.0
が、
そしてxが+nan.0
なら、+nan.0
と+nan.0
が返されます。
(この動作はsrfi-144では指定されていません。GaucheはPOSIXのmodf
の動作に
準じています)。
(flinteger-fraction fl-pi)
⇒ 3.0 and 0.14159265358979312
[R7RS flonum] {scheme.flonum} flonum xの指数部をflonumとして返します。xがゼロでない有限値なら、 結果は整数です。
xがゼロなら、-inf.0
が、
xが無限大なら、+inf.0
が返されます。
(flexponent 1.0) ⇒ 0.0 (flexponent 1024.0) ⇒ 10.0 (flexponent 0.01) ⇒ -7.0 (flexponent fl-least) ⇒ -1074.0
[R7RS flonum]
{scheme.flonum}
flexponent
と同様にxの指数部を返しますが、こちらは正確な整数を返します。
xがゼロの場合、定数fl-integer-exponent-zero
が返されます。
xが無限大の場合、非常に大きな正確な整数
((ldexp 1 (flinteger-exponent +inf.0))
が+inf.0
になるような
値)が返されます。
xが+nan.0
の場合、定数fl-integer-exponent-nan
が返されます。
[R7RS flonum]
{scheme.flonum}
flonum xの正規化された仮数部 (符号はxと同じ)をflonumで、
およびxの指数部を正確な整数で2つの返り値として返します。
それらをyとnとすれば、x = (* y (expt 2 n))
であり、
x = (ldexp y n)
となります。
この手続きはGaucheのfrexp
と同じです(数値の変換参照)。
xがゼロでない有限の値ならば、
最初の返り値は0.5以上で1.0より小さな値になります。
コーナーケースについてはsrfi-144では規程されていませんが、Gaucheは
frexp
の仕様に沿って、次のようにしています。
xが0.0
(か-0.0
)なら、結果は0.0
(か-0.0
)と0
。
xが無限大なら、同じ符号の無限大と0
。
xが+nan.0
なら、+nan.0
と0
。
(flnormalized-fraction-exponent 12345.6789)
⇒ 0.7535204406738282 and 14
(make-flonum 0.7535204406738282 14)
⇒ 12345.6789
[R7RS flonum]
{scheme.flonum}
xが正もしくは0.0
なら0
を、
負もしくは-0.0
なら1
を返します。
(flsign-bit +nan.0)
は実装依存です。
ビット値でなく符号が欲しい場合、flsgn
も使えます。
(註: flonum?
は組み込みです。数値に関する述語参照)。
[R7RS flonum]
{scheme.flonum}
flonum専用の比較関数です。それぞれ
=
、<
、>
、<=
、>=
に対応します。
今のところGaucheではこれらは単に汎用の比較関数の別名となっています。 なのでflonum以外の数値が渡されても動作しますが、そうすることはポータブルではありません。 これらの関数の意図は、将来的にコンパイラに型についてのヒントを与え 最適化を可能にすることです。
-0.0
と0.0
は数値比較においては等しいものと扱われることに
注意してください。(fl<? -0.0 0.0)
は#f
になります。
引数に+nan.0
がひとつでもあれば、結果は常に#f
となります。
[R7RS flonum]
{scheme.flonum}
xとyの少なくとも一方が+nan.0
であれば#t
を、
そうでなければ#f
を返します。
[R7RS flonum]
{scheme.flonum}
xが整数のflonumであれば#t
を、
そうでなければ#f
を返します。
[R7RS flonum]
{scheme.flonum}
flonum専用のzero?
、positive?
、negative?
です。
(flnegative? -0.0)
は#f
であることに注意してください。
負のゼロを普通のゼロと見分けるにはflsign-bit
やflsgn
を使います。
[R7RS flonum]
{scheme.flonum}
flonum専用のodd?
とeven?
です。
xが整数でなければエラーが投げられます。
[R7RS flonum]
{scheme.flonum}
それぞれflonum専用のfinite?
、infinite?
、
nan?
です (数値に関する述語参照)。
[R7RS flonum]
{scheme.flonum}
それぞれ、xが正規化浮動小数点数、非正規化浮動小数点数の場合に
#t
を返し、そうでなければ#f
を返します。
[R7RS flonum]
{scheme.flonum}
flonum専用のmin
とmax
です。汎用のmin
、max
と
異なり、引数がゼロ個でもエラーにならず、それぞれ-inf.0
と
+inf.0
が返されます。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
flonum専用の+.
と-.
です (数値の演算参照)。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
(+ (* x y) z)
の値を返しますが、
プラットフォームによってはより速く正確な結果を返します。
Gaucheは内部的にはC99のfma
を呼んでいます。
「より正確」というのは、乗算と加算が一度に行われ、丸めが最後に1回だけ起きることを
指します。
註: 0.9.8リリース時点で、MinGWのfma
実装は
乗算と加算それぞれに丸めが入るようになっているので、
しばしばわずかにずれた結果を返します。
[R7RS flonum]
{scheme.flonum}
flonum専用の-.
と/.
です (数値の演算参照)。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
flonum専用のabs
です (数値の演算参照)。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
(abs (- x y))
を返します。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
(max (- x y) 0)
を返します。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
xの符号ビットが0 (ゼロまたは正)なら1.0
を、
1 (負および負のゼロ)の場合は-1.0
を返します。
(flcopysign 1.0 x)
や(if (zero? (flsign-bit x)) 1.0 -1.0)
と考えることもできます。
(flsgn 0.0)
の結果は1.0
、
(flsng -0.0)
の結果は-1.0
です。
+nan.0
を渡した場合の結果は実装上NaNの符号ビットがどうなっているかに
よるので、結果は1.0
か-1.0
かのいずれかになりますが、
どちらになるかはっきりしたことは言えません。
符号つきflonumではなく符号ビットそのものを取り出すにはflsign-bit
が使えます。
[R7RS flonum]
{scheme.flonum}
flonum専用のnumerator
とdenominator
です
(数値の演算参照)。
無限大とゼロのdenominatorは1.0
になります。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
flonum専用のfloor
、ceiling
、round
、truncate
です
(数値の演算参照)。
引数がflonumかどうかはチェックしていませんが、flonum以外の引数を渡すことは ポータブルではありません。
[R7RS flonum]
{scheme.flonum}
flonum専用のexp
です (数値の演算参照)。
(expt fl-e x)
を返します。
[R7RS flonum]
{scheme.flonum}
(expt 2 x)
を返します。
[R7RS flonum]
{scheme.flonum}
(- 1 (expt fl-e x))
を返しますが、xが小さい場合はより正確です。
内部的にはCのexpm1
を呼んでいます。
[R7RS flonum]
{scheme.flonum}
(*. x x)
を返します。
[R7RS flonum]
{scheme.flonum}
flonum専用のsqrt
です。
[R7RS flonum] {scheme.flonum} flonum xの3乗根を返します。
[R7RS flonum]
{scheme.flonum}
(sqrt (* x x) (* y y))
を計算しますが、
中間結果でオーバーフローやアンダーフローが起きないように配慮されています。
[R7RS flonum]
{scheme.flonum}
flonum専用のexpt
です(数値の演算参照)。
[R7RS flonum]
{scheme.flonum}
flonum専用のlog
です(数値の演算参照)。
[R7RS flonum]
{scheme.flonum}
(log (+ x 1))
を返しますが、xがゼロに近い場合により正確です。
[R7RS flonum] {scheme.flonum} flonum xの2および10を底とする対数を返します。
[R7RS flonum] {scheme.flonum} xを底とする対数を計算する手続きを返します。xが1.0より大きい実数でない 場合はエラーが投げられます。
(define log5 (make-fllog-base 5.0)) (log5 25.0) ⇒ 5.0
[R7RS flonum]
{scheme.flonum}
それぞれ、flonum専用の
sin
, cos
, tan
,
asin
, acos
, sinh
, cosh
, tanh
,
asinh
, acosh
, atanh
です
(数値の演算参照)。
flatan
は1つまたは2つの引数を取ります。(flatan y x)
は
(flatan (/ y x))
を計算します (が、より正確です)。
[R7RS flonum]
{scheme.flonum}
(fltruncate (fl/ x y))
を返します。
結果は常に整数のflonumになります。引数x、yは整数である必要はありません。
(flquotient 14.0 4.0) ⇒ 3.0 (flquotient -14.0 4.0) ⇒ -3.0 (flquotient 14.0 2.5) ⇒ 5.0 (flquotient -14.2 2.8) ⇒ -5.0
[R7RS flonum]
{scheme.flonum}
(- x (* y (flquotient x y)))
を返します。
(flquotient 14.0 4.0) ⇒ 2.0 (flquotient -14.0 4.0) ⇒ -2.0 (flquotient 14.0 2.5) ⇒ 1.5 (flquotient -14.2 2.8) ⇒ -0.1999999999999993 ; inexact calculation
[R7RS flonum] {scheme.flonum} 二つの値を返します:
(flremainder x y)
と同じですが、異なる計算方法を使うので
最後のビットで差が出るかもしれません)。
これはC99のremquo
に相当します。
周期関数の入力を対称性を使って絞る時に便利です。
[R7RS flonum] {scheme.flonum} flonum xのガンマ関数を計算します。 xが整数の時は、x-1の階乗と一致します。
これはGauche組み込みのgamma
と同じです (数値の演算参照)。
[R7RS flonum]
{scheme.flonum}
(log (abs (flgamma x)))
の値および、(flgamma x)
の符号
(正の場合は1.0、負の場合は-1.0)の二つの値を返します。
最初の値はGauche組み込みのlgamma
で計算されます (数値の演算参照)。
これはgamma
を計算してlog
を取るより正確です。
なお、xが-inf.0
か+nan.0
の場合、二番目の値は+nan.0
になります。
[R7RS flonum] {scheme.flonum} n階の第1種ベッセル関数の値を返します。
[R7RS flonum] {scheme.flonum} n階の第2種ベッセル関数の値を返します。
[R7RS flonum] {scheme.flonum} 誤差関数erfです。
[R7RS flonum] {scheme.flonum} 1 - erf(x) を計算します。
Next: R7RS combinator formatting, Previous: R7RS flonum, Up: R7RS large [Contents][Index]
scheme.bytevector
- R7RS bytevectorこのモジュールはR6RSの(rnrs bytevectors)
からの輸入です
(R7RSのライブラリ名は単数形であることに注意)。
Gaucheでは「バイトベクタ」はu8vectorです。
以下の手続きはgauche.uvector
のものと同じです。
詳しくはバイトベクタ互換性を参照してください。
bytevector? make-bytevector bytevector-length bytevector=? bytevector-fill! bytevector-copy bytevector-u8-ref bytevector-u8-set! bytevector-s8-ref bytevector-s8-set! bytevector->u8-list u8-list->bytevector
以下の手続きはgauche.unicode
のものと同じです。
詳しくはUnicodeユーティリティを参照してください。
string->utf8 string->utf16 string->utf32 utf8->string utf16->string utf32->string
このモジュールはbytevector-copy!
をexportしていますが、
R7RS baseおよびgauche.unicode
に含まれる同名の手続きと引数の順序が異なります。
gauche.uvector
でbytevector-copy!-r6
として定義されているものと
同じになります(バイトベクタ互換性参照)。
[R7RS bytevector] symbolがエンディアンを示すシンボルであればそれをそのまま返し、 そうでなければマクロ展開時にエラーを投げます。 エラーを早期に検出するのに役立ちます。 また、シンボルがエンディアンを示すという意図を明確にできます。
有効なシンボルはエンディアンネスを参照してください。
big
とlittle
だけがポータブルです。
[R7RS bytevector]
システムのネイティブなエンディアンを示すシンボルを返します。
Gauche組み込みのnative-endian
と同じです(エンディアンネス参照)。
[R7RS bytevector]
u8vector bvの、pos番めのオクテットからsizeオクテットを、
それぞれ符号なしと符号あり整数として読み取ります。
binary.io
モジュールのget-uint
とget-sint
に似ていますが、
引数の順序は異なります。
endianにはエンディアンを示すシンボルを渡します。
bit
とlittle
のみがポータブルに使えます。
[R7RS bytevector]
それぞれ、符号なし、および符号つき整数valをu8vector bvの
pos番目のオクテットからsizeオクテットにわたって格納します。
binary.io
のput-uint!
およびput-sint!
と
引数の順序以外は同じです。
endian引数には、Gaucheが許すエンディアンを示すシンボルなら何でも渡せますが、
ポータブルなコードで使えるのはbig
とlittleだけです。
[R7RS bytevector] u8vector bvからsizeオクテットづつ取り出し、それをそれぞれ符号なし、 および符号つき整数と解釈して、その整数のリストを返します。 bvの長さがsizeの整数倍でなければエラーが投げられます。
endian引数には、Gaucheが許すエンディアンを示すシンボルなら何でも渡せますが、
ポータブルなコードで使えるのはbig
とlittleだけです。
[R7RS bytevector]
lisはそれぞれ符号なしまたは符号つき整数のリストでなければなりません。
(* size (length lis))
のu8vectorが作られて、
先頭からsizeオクテットごとに、リストの整数が詰められてゆきます。
そしてそのu8vectorが返されます。
endian引数には、Gaucheが許すエンディアンを示すシンボルなら何でも渡せますが、
ポータブルなコードで使えるのはbig
とlittleだけです。
[R7RS bytevector]
Retrieve a numerical value from a u8vector bv starting at
index k, using endian. These are the same as
get-u16
, … in binary.io
, except that
all arguments are mandatory. (Note that
ieee-single
and ieee-double
corresponds to f32
and
f64
). See バイナリI/O), for the details.
They accept all valid endianness symbols in Gauche, but
the portable code should only use big
and little for
endian argument.
[R7RS bytevector]
Like bytevector-u16-ref
etc., but uses native endianness.
[R7RS bytevector]
Store a numerical value into a u8vector bv starting at
index k, using endian. These are the same as
put-u16!
, … in binary.io
, except that
all arguments are mandatory. (Note that
ieee-single
and ieee-double
corresponds to f32
and
f64
). See バイナリI/O), for the details.
They accept all valid endianness symbols in Gauche, but
the portable code should only use big
and little for
endian argument.
[R7RS bytevector]
Like bytevector-u16-set!
etc., but uses native endianness.
Previous: R7RS bytevectors, Up: R7RS large [Contents][Index]
scheme.show
- R7RS combinator formattingExports bindings of R7RS (scheme show)
library.
From R7RS programs, those bindings are available by
(import (scheme show))
.
scheme.show is a combination of submodules scheme.show.base, scheme.show.color, scheme.show.columnar and scheme.show.unicode.
Exports bindings of R7RS (scheme show base)
library.
From R7RS programs, those bindings are available by
(import (scheme show base))
.
This contains most combinator formatting procedures.
Exports bindings of R7RS (scheme show color)
library.
From R7RS programs, those bindings are available by
(import (scheme show color))
.
This contains formatters to color text using ANSI escape codes.
Exports bindings of R7RS (scheme show columnar)
library.
From R7RS programs, those bindings are available by
(import (scheme show columnar))
.
This contains formatters to help format in columns.
Exports bindings of R7RS (scheme show unicode)
library.
From R7RS programs, those bindings are available by
(import (scheme show unicode))
.
Combinator formatting provides a functionality similar to
format
from SRFI-28. But instead of writing a template string,
you can use S-expressions, which are called “formatters”. It’s also
extensible.
The two main concepts in combinator formatting are formatters and
states. Formatters are procedures that specify how or what you want to
output. Formatters can be composed to produce complex format. Normal
types are also accepted where a procedure takes a formatter, they are
formatted with displayed
.
Format states let us customize control formatting, for example how
many precision digits, what character for padding …. Format
states can be changed locally with with
or with!
.
The entry point to combinator formatting is show
, which takes a
sequence of formatters and outputs to a port or returns a string.
[R7RS show base]
{scheme.show.base}
This is the main entry for combinator formatting. All formatters are
processed to produce a string to output-dest if it’s a port. If
output-dest is #f
, the output string is returned.
show
return value is otherwise undefined. Non-formatters are
also accepted and will be wrapped in displayed
formatter.
(show #f "π = " (with ((precision 2)) (acos -1)) nl) ⇒ "π = 3.14\n"
[R7RS show base]
{scheme.show.base}
The formatter that formats the object obj the
same as display
. This is the default formatter when you pass an
object to show
.
[R7RS show base]
{scheme.show.base}
The formatter that formats the object obj the same as
write
. Formatting settings numeric
and precision
are respected for relevant number types as long as the result can
still be passed to read
.
[R7RS show base]
{scheme.show.base}
Similar to written
but does not handle shared structures.
[R7RS show base] {scheme.show.base} Pretty prints an object.
[R7RS show base]
{scheme.show.base}
Similar to pretty
but does not handle shared structures.
[R7RS show base]
{scheme.show.base}
Prints a string, adding esc-ch (#\\
by default) in front
of all quote-ch (#\"
by default).
If esc-ch is #f
, escape all quote-ch by doubling
it.
If renamer is specified, it’s a procedure that takes one
character and returns another character or #f
. It serves two
purposes: to allow quoting more than one character and to replace them
with something else. If the procedure returns #f
, the character
in question is not escaped. Otherwise the character is escaped and
replaced with a new one.
esc-ch could also be a string, but this is Gauche specific behavior.
(show #t (escaped "hello \"world\"")) ⇒ hello \"world\" (show #t (escaped "hello \"world\"" #\l)) ⇒ he\l\lo "world" (show #t (escaped "hello \"world\"" #\l #\x)) ⇒ hexlxlo "worxld" (show #t (escaped "hello \"world\"" #\e #f (lambda (x) (if (char=? x #\o) #\O #f)))) ⇒ heelleO "weOrld"
[R7RS show base]
{scheme.show.base}
Determines if str needs to be escaped or not. If true, the
string is wrapped with quote-ch. The original string is escaped
with escaped
.
The string needs to be escaped if any quote-ch or esc-ch
is present, or any character that makes pred return #t
.
(show #t (maybe-escaped "helloworld" char-whitespace?)) ⇒ helloworld (show #t (maybe-escaped "hello world" char-whitespace?)) ⇒ "hello world" (show #t (maybe-escaped "hello \"world\"" char-whitespace? #\")) ⇒ "hello \"world\""
[R7RS show base] {scheme.show.base} Formats a number. The default values are from state variables below.
(show #f (numeric 1000)) ⇒ "1000" (show #f (numeric 1000 8)) ⇒ "1750" (show #f (numeric 1000 8 2)) ⇒ "1750.00" (show #f (numeric 1000 8 2 #t)) ⇒ "+1750.00" (show #f (numeric -1000 8 2 (cons "(" ")"))) ⇒ "(1750.00)" (show #f (numeric 1000 8 2 #t 2)) ⇒ "+17,50.00" (show #f (numeric 1000 8 2 #t 2 #\')) ⇒ "+17'50.00" (show #f (numeric 1000 8 2 #t 2 #\' #\:)) ⇒ "+17'50:00"
[R7RS show base]
{scheme.show.base}
Formats a number with default comma-rule 3. See numeric
for details.
(show #f (numeric/comma 1000)) ⇒ "1,000" (show #f (with ((comma-sep #\.)) (numeric/comma 1000))) ⇒ "1.000"
[R7RS show base] {scheme.show.base} Formats a numeric with SI suffix. The default base is 1024 and uses suffix names like Ki, Mi, Gi…. Other bases (e.g. 1000) use suffixes k, M, G…. If separator is specified, it’s inserted between the number and suffix.
(show #f (numeric/si 1024)) ⇒ "1Ki" (show #f (numeric/si 200000 1000)) ⇒ "200k" (show #f (numeric/si 1024 1024 #\/)) ⇒ "1/Ki"
[R7RS show base]
{scheme.show.base}
Like numeric
but if the result does not fit in width
characters with current precision, outputs a string of hashes instead
of the truncated and incorrect number.
(show #f (with ((precision 2)) (numeric/fitted 4 1.25))) ⇒ "1.25" (show #f (with ((precision 2)) (numeric/fitted 4 12.345))) ⇒ "#.##"
Outputs a newline.
(show #f nl) ⇒ "\n"
Short for “fresh line”, make sures the following output is at the beginning of the line.
(show #f fl) ⇒ "" (show #f "aaa" fl) ⇒ "aaa\n"
Outputs nothing. This is useful in combinators as default no-op in conditionals.
[R7RS show base] {scheme.show.base} Appends pad-char to reach the given column.
(show #f "abcdef" (space-to 3) "a") ⇒ " a" (show #f "abcdef" (space-to 3) "a") ⇒ "abcdefa"
[R7RS show base] {scheme.show.base} Outputs pad-char to reach the next tab stop.
[R7RS show base] {scheme.show.base}
[R7RS show base] {scheme.show.base}
[R7RS show base] {scheme.show.base} Formats each element in list with mapper and inserts sep in between. sep by default is an empty string, but it could be any string or formatter.
(show #f (joined displayed (list "a" "b") " ")) ⇒ "a b" (show #f (joined displayed (list "a" "b") nl)) ⇒ "a\nb"
[R7RS show base]
{scheme.show.base}
Similar to joined
except the separator is inserted before
every element.
(show #f (joined/prefix displayed '(usr local bin) "/")) ⇒ "/usr/local/bin"
[R7RS show base]
{scheme.show.base}
Similar to joined
except the separator is inserted after every
element.
(show #f (joined/suffix displayed '(1 2 3) nl)) ⇒ "1\n2\n3\n"
[R7RS show base]
{scheme.show.base}
Similar to joined
but last-mapper is used on the last
element of list instead.
(show #f (joined/last displayed (lambda (last) (each "and " last)) '(lions tigers bears) ", ")) ⇒ "lions, tigers, and bears"
[R7RS show base]
{scheme.show.base}
Similar to joined
but if list is a dotted list, then
formats the dotted value with dot-mapper instead.
[R7RS show base] {scheme.show.base}
[R7RS show base] {scheme.show.base} Pads the output of fmt… on the left side with pad-char if it’s shorter than width characters.
(show #f (padded 10 "abc")) ⇒ " abc" (show #f (with ((pad-char #\-)) (padded 10 "abc"))) ⇒ "-------abc"
[R7RS show base]
{scheme.show.base}
Similar to padded
but except padding is on the right side instead.
[R7RS show base]
{scheme.show.base}
Similar to padded
but except padding is on both sides, keeping
the fmt output at the center.
[R7RS show base] {scheme.show.base} Trims the output of fmt… on the left so that the length is width characters or less. If ellipsis state variable is defined, it will be put on the left to denote trimming.
(show #f (trimmed 5 "hello world")) ⇒ "world" (show #f (with ((ellipsis "..")) (trimmed 5 "hello world"))) ⇒ "..rld"
[R7RS show base]
{scheme.show.base}
Similar to trimmed
but the trimming is on the right.
[R7RS show base]
{scheme.show.base}
Similar to trimmed
but the trimming is on both sides, keeping
the center of the output.
[R7RS show base]
{scheme.show.base}
A variant of trimmed
which generates each fmt
in left to
right order, and truncates and terminates immediately if more than
width
characters are generated. Thus this is safe to use with
an infinite amount of output, e.g. from written-simply
on an
infinite list.
[R7RS show base]
{scheme.show.base}
A combination of padded
and trimmed
, ensures the output
width is exactly width, truncating if it goes over and padding
if it goes under.
[R7RS show base]
{scheme.show.base}
A combination of padded/right
and trimmed/right
, ensures
the output width is exactly width, truncating if it goes over
and padding if it goes under.
[R7RS show base]
{scheme.show.base}
A combination of padded/both
and trimmed/both
, ensures
the output width is exactly width, truncating if it goes over
and padding if it goes under.
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show columnar] {scheme.show.columnar}
[R7RS show color] {scheme.show.color} Outputs the ANSI escape code to make all fmt… a given color or style.
[R7RS show unicode] {scheme.show.unicode}
[R7RS show unicode] {scheme.show.unicode}
[R7RS show base]
{scheme.show.base}
This is short for “function” and the analog to lambda
. It
returns a formatter which on application evaluates each expr and
fmt in left-to-right order, in a lexical environment extended
with each identifier id bound to the current value of the state
variable named by the symbol state-var. The result of the
fmt is then applied as a formatter.
As a convenience, any (id state-var)
list may be abbreviated as
simply id
, indicating id is bound to the state variable
of the same (symbol) name.
[R7RS show base]
{scheme.show.base}
This is the analog of let
. It temporarily binds specified state
variables with new values for fmt ….
[R7RS show base]
{scheme.show.base}
Similar to with
but the value updates persist even after
with!
.
[R7RS show base] {scheme.show.base} Calls fmt1 on (a conceptual copy of) the current state, then fmt2 on the same original state as though fmt1 had not been called (i.e. any potential state mutation by fmt1 does not affect fmt2).
[R7RS show base]
{scheme.show.base}
A utility, calls formatter on a copy of the current state (as with
forked
), accumulating the results into a string. Then calls the
formatter resulting from (mapper result-string)
on the original
state.
The current port output is written into, could be overriden to capture intermediate output.
The current row of the output.
The current column of the output.
The current line width, used for wrapping, pretty printing and columnar formatting.
The underlying standard formatter for writing a single string.
The default value outputs the string while tracking the current row and col. This can be overridden both to capture intermediate output and perform transformations on strings before outputting, but should generally wrap the existing output to preserve expected behavior.
The mapper for automatic formatting of non-string/char values in top-level show, each and other formatters. Default value is implementation-defined.
A function of a single string. It returns the length in columns of that string, used by the default output.
The character used for by padding formatters, #\space
by default
The string used when truncating as described in trimmed
.
The radix for numeric output, 10 by default. Valid values are from 2 to 36.
The number of digits written after the decimal point for numeric output. The value is rounded if the numeric value written out requires more digits than requested precision. See the SRFI for exact rounding behavior.
If #t
, always output the plus sign +
for positive
numbers. If sign-rule is a par of two strings, negative numbers
are printed with the strings wrapped around (and no preceding negative
sign -
).
The number of digits between commans, specified by comma-sep.
The character used as comma for numeric formatting, #\,
by
default.
The character to use for decimals in numeric formatting. The default
depends on comma-sep, if it’s #\.
, then the decimal
separator is #\,
, otherwise it’s #\.
Previous: R7RS bytevectors, Up: R7RS large [Contents][Index]