Previous: R7RS small language, Up: ライブラリモジュール - R7RS標準ライブラリ [Contents][Index]
R7RS largeはまだ策定途上ですが、既に決定したライブラリについては 徐々にGaucheに追加してゆきます。
現在、R7RS-largeには2つのエディション (RedとTangerine) が 制定されています。その中に定義されているライブラリのうち、 以下のものはまだサポートされていません:
scheme.ilist scheme.rlist scheme.text scheme.stream scheme.ephemeron scheme.regex scheme.bytevector scheme.vector.TAG scheme.show
以下のライブラリがサポートされているものです:
• R7RSリスト: | scheme.list
| |
• R7RSベクタ: | scheme.vector
| |
• R7RSソート: | scheme.sort
| |
• R7RSセット: | scheme.set
| |
• R7RS文字セット: | scheme.charset
| |
• R7RSハッシュテーブル: | scheme.hash-table
| |
• R7RS変更不可な両端キュー: | scheme.ideque
| |
• R7RSジェネレータ: | scheme.generator
| |
• R7RS遅延シーケンス: | scheme.lseq
| |
• R7RSボックス: | scheme.box
| |
• R7RSリストキュー: | scheme.list-queue
| |
• R7RS比較器: | scheme.comparator
| |
• R7RSマップ: | scheme.mapping
| |
• R7RS整数除算: | scheme.division
| |
• R7RSビット演算: | scheme.bitwise
| |
• R7RS fixnum: | scheme.fixnum
| |
• R7RS flonum: | scheme.flonum
|
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).
[SRFI-1] {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} Creates a vector of length length, filling elements left to right by calling f repeatedly.
The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.
(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))
[R7RS vector] {scheme.vector} Creates a vector of length length, filling elements right to left by calling f repeatedly.
The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.
(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} Copies the vector vec with reversing its elements. Optional start and end arguments can limit the range of the input.
(vector-reverse-copy '#(a b c d e) 1 4) ⇒ #(d c b)
[R7RS vector]
{scheme.vector}
Same as (apply vector-append list-of-vectors)
.
[R7RS vector]
{scheme.vector}
The number of arguments must be multiple of 3. The argument list
must be in the following format, where each vecN is
a vector, and startN
and endN
are nonnegative integers:
vec1 start1 end1 vec2 start2 end2 …
This procedure creates a new vector by concatenating subvectors specified by each triplet. That is, it works as if it’s the following code, except it avoids copying each subvector:
(vector-append (vector-copy vec1 start1 end1) (vector-copy vec2 start2 end2) …)
Here’s an example:
(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}
Returns #t
if vec’s length is zero, and
#f
if vec’s length is more than zero.
Signals an error if vec is not a vector.
[R7RS vector]
{scheme.vector}
Compares vecs element-wise, using given predicate elt=.
Returns #t
iff lengths of all the vectors are the same,
and every corresponding elements are equal by elt=.
Elt= is always called with two arguments and must
return #t
iff two are the same.
[R7RS vector]
{scheme.vector}
Kons is a procedure that takes n+1 arguments, where
n is the number of given vectors. For each element of the
given vectors, kons is called as
(kons seed e_1i e_2i …)
,
where and e_ni is the i-th element
of the vector n. If the lengths of the vectors differ,
iteration stops when the shortest vector is exhausted.
The initial value of seed is knil, and the return value
from kons is used as the next seed value. The last return value
of kons is returned from vector-fold
.
The iteration is strictly left to right.
Note that the seed value precedes elements, which is opposite to
fold
(see コレクションに対するマッピング). It’s an unfortunate
historical glitch; vector-fold-left
would be more consistent
name.
(vector-fold (^[a b] (cons b a)) '() '#(a b c d)) ⇒ (d c b a)
[R7RS vector]
{scheme.vector}
Like vector-fold
, but elements in the vec1 vec2 …
are visited from right to left.
Unlike fold-right
(see シーケンス上のマップ),
the procedure kons takes the accumulated value in the
first argument.
(vector-fold-right (^[a b] (cons b a)) '() '#(a b c d)) ⇒ (a b c d)
[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.sort
- R7RSソートProvides utilities to sort, and to work on sorted lists/vectors.
This module is the same as srfi-132
.
Gauche has built-in sort and merge procedures (see ソートとマージ). This module has a bit different API. Notably, the ordering predicate comes first than the sequence to be sorted, and the procedures dealing with vectors uniformly support start/end arguments
This module also provide useful procedures working on sorted or partially sorted sequences.
[R7RS sort] {scheme.sort} Sort elements in a list lis according to the ordering predicate elt<, which takes two elements from lis and returns true iff the first argument is strictly less than the second argument.
Returns a sorted list.
The procedures with bang are allowed, but not required, to reuse
lis
. The “stable” variation guarantees stable sort.
These are basically the same as Gauche’s built-in
sort
, sort!
, stable-sort
and stable-sort!
,
except the Gauche’s version works on any sequences and takes
arguments differently. (See ソートとマージ.)
[R7RS sort] {scheme.sort} Returns true if the list list is sorted according to the ordering predicate elt<.
See also sorted?
in ソートとマージ.
[R7RS sort] {scheme.sort} Given two sorted lists lis1 and lis2, returns a new sorted list according to the ordering predicate elt<.
Note that list-merge!
works in-place, that is, all the pairs
in lis1 and lis2 are reused.
See also merge
and merge! in ソートとマージ.
[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 (exlusive) 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 (exlusive) 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 (exlusive) 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 lenght 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 mathing 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} Returs 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!
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}
{scheme.charset}
[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}
[R7RS charset] {scheme.charset}
Next: 文字セットへの問い合わせ, Previous: 文字セットの比較, Up: R7RS文字セット [Contents][Index]
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
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に含まれる文字をリストあるいは文字列にして返します。 大きな文字セットに適用する場合は注意してください。
Next: 定義済みの文字セット, Previous: 文字セットへの問い合わせ, Up: R7RS文字セット [Contents][Index]
[R7RS charset] {scheme.charset} char-setにchar1 …を加えた文字集合を返します。
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
Previous: 文字セットに適用できる代数的関数, Up: R7RS文字セット [Contents][Index]
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
[R7RS charset] {scheme.charset}
Next: R7RS変更不可な両端キュー, Previous: R7RS文字セット, Up: R7RS large [Contents][Index]
scheme.hash-table
- R7RSハッシュテーブルThis module provides hash table library, originally defined as srfi-125
.
Hash table provided with this module is the same as Gauche’s built-in hash table. However, srfi-125 introduces procedures that conflict with Gauche’s original procedures, so Gauche provides those procedures built-in but under aliases. See ハッシュテーブル, for the built-in hash table procedures.
With this module, procedures are provided as defined in R7RS. Use this module when you’re writing portable code.
Srfi-125 also defines compatiblity procedures with srfi-69, although saying they’re deprecated. Those deprecated procedures are supported in this module, too.
The following procedures are the same as Gauche’s built-in.
hash-table-unfold 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-differnce! hash-table-xor!
See ハッシュテーブル, for the description of those procedures.
The following procedures are also provided as Gauche’s built-in,
but with -r7
suffix.
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}
This enhances built-in make-hash-table
with the second form,
that takes two procedures instead of one comparator, as srfi-69.
In the srfi-69 form, equal-proc is a procedure taking two keys to see if they are the same, and hash-proc is a procedure taking a key to calculate its hash value (nonnegative fixnum). The compatibility form is deprecated and should be avoided in the new code.
The optional arg dots are ignored in Gauche.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-r7
(see ハッシュテーブル).
Construct a new hash table with key-comparator cmpr. It is populated by key value …, which is a list with keys and values appear alternatively. It is an error if the length of key-value list is not even.
Note that srfi-125 defines this procedure to return an immutable hash table if the implementation supports one. Gauche doesn’t provide immutable hash tables (we do have immutable map instead, see 変更不可なマップ), but when you’re writing a portable program, be careful not to modify the table returned by this procedure.
[R7RS hash-table]
{scheme.hash-table}
This enhances built-in alist->hash-table
with the second form,
that takes two procedures instead of one comparator, as srfi-69.
In the srfi-69 form, equal-proc is a procedure taking two keys to see if they are the same, and hash-proc is a procedure taking a key to calculate its hash value (nonnegative fixnum). The compatibility form is deprecated and should be avoided in the new code.
The optional arg dots are ignored in Gauche.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-delete!-r7
.
Deletes entries associated with the given keys from the table ht. It is ok if ht doesn’t have key. Returns the number of entries that are actually deleted.
It differs from built-in hash-table-delete!
in two points:
The bulit-in one can take exactly one key, and returns
a boolean indicating if the entry is actually deleted.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-intern!-r7
.
Search key in ht. If it is found, returns the associated value. If it is not found, call failure without artuments, and insert a new entry associating key and the value failure returns, and returns that new value.
[R7RS hash-table]
{scheme.hash-table}
This is the same as built-in hash-table-update!-r7
.
It takes differnt 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.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 bidrectional 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.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 genreator-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 futher 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 oridnary 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.box
- R7RSボックスThis module defines the box datatype, which is a simple container that can hold one Scheme object. It can be used as a minimal data storage, or a sort of mutable indirect “pointer”.
Traditionally a pair (with ignoring its cdr) or a single-element vector has been used for this purpose; in modern Scheme you can also define a record type with one mutable field. Nevertheless, a box is very common abstraction to describe various algorithms, and having common interface to it is useful.
The srfi leaves some details to implementations. Here are our choices:
equal?
compares their contents when
two are not eqv?
. In the spec, when two boxes are eqv?
then they must also be equal?
to each other, but it’s up
to the implementation when two are not eqv?
.
When you’re writing portable code, be careful not to depend on the
equal?
behavoir.
[R7RS box]
{scheme.box}
Returns a fresh box object that contains the value val
.
[R7RS box]
{scheme.box}
Returns #t
iff obj is a box object.
[R7RS box] {scheme.box} Returns box’s content.
[R7RS box] {scheme.box} Mutate box’s content with val. Returns unspecified value.
Next: R7RS比較器, 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.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.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 maked 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
ascendnig 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.
Argments 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.
Argments 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-quotinet
、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) where roundup(x) is ceiling(x) if x - floor(x) <= 0.5 and floor(x) if 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
と同じです。
整数に対するビット操作参照。
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}
The same as (flonum-epsilon)
in Gauche
(see 数値の比較).
[R7RS flonum]
{scheme.flonum}
If this is #t
, (fl+* x y z)
executes as fast as,
or faster than, (fl+ (fl* x y) z)
. If not, #f
.
[R7RS flonum]
{scheme.flonum}
These are exact integer values returned in special occasion
from flinteger-exponent
. The values themselves don’t mean
much; they’re to be compared with the result of flinteger-exponent
.
If its argument is 0, flinteger-exponent
returns
fl-integer-exponent-zero
. If its argument is +nan.0
,
flinteger-exponent
returns fl-integer-exponent-nan
.
[R7RS flonum] {scheme.flonum} Returns a flonum that’s equal to or (if there’s no equivalent flnoum) the closest to number.
If number is not a real number,
+nan.0
is returned. (Note: srfi-144 recommends it, but
a conformant implementation may signal an error in such case.
Portable code shouldn’t rely on this behavior.)
[R7RS flonum] {scheme.flonum} Returns a flonum adjacent to x in the direction of y. If x = y, x is returned.
(fladjacent 1.0 2.0) ⇒ 1.0000000000000002 (fladjacent 1.0 0.0) ⇒ 0.9999999999999999
[R7RS flonum]
{scheme.flonum}
Returns a flonum whose absolute value is (abs x)
and whose
sign is the sign of y.
[R7RS flonum]
{scheme.flonum}
Returns a flonum (* x (expt 2 n))
. It is the same
as ldexp
(see 数値の変換).
[R7RS flonum]
{scheme.flonum}
Returns two flnoums, the integral part of x and the fractional
part of x. Same as modf
(see 数値の変換).
If x is +inf.0
, +inf.0
and 0.0
is returned.
If x is -inf.0
, -inf.0
and -0.0
is returned.
If x is +nan.0
, +nan.0
and +nan.0
is returned.
(These corner cases are not explicit in srfi-144, but follows
the POSIX specification of modf
.)
(flinteger-fraction fl-pi)
⇒ 3.0 and 0.14159265358979312
[R7RS flonum] {scheme.flonum} Returns the exponent part of x as a flnoum. If x is a non-zero finite value, the result is an integer.
If x is zero, -inf.0
is returned.
If x is +inf.0 or -inf.0
, +inf.0
is returned.
(flexponent 1.0) ⇒ 0.0 (flexponent 1024.0) ⇒ 10.0 (flexponent 0.01) ⇒ -7.0 (flexponent fl-least) ⇒ -1074.0
[R7RS flonum]
{scheme.flonum}
Returns the same as flexponent
but as an exact integer.
If x is zero, the value of fl-integer-exponent-zero
is
returned.
If x is +inf.0 or -inf.0
, a large
implementation-dependent exact integer (usually it’s so large that
(ldexp 1 (flinteger-exponent +inf.0))
becomes +inf.0
)
is returned.
If x is +nan.0
, the value of
fl-integer-exponent-nan
is returned.
[R7RS flonum]
{scheme.flonum}
Returns two flonums, a normalized mantissa of x with the same sign
as x, and an exact integer exponent of x. If it returns
a value y and n, x = (* y (expt 2 n))
and
x = (ldexp y n)
.
This is the same as frexp
(see 数値の変換).
If x is non-zero finite value, the first value
falls between 0.5 (inclusive) and 1.0 (exclusive).
The corner cases are not explicit in srfi-144, but Gauche follows
frexp
: If x is (minus) zero, it returns
(minus) zero and 0; if x is infinity, it returns
infinity (of the same sign) and 0; if x is NaN,
it returns NaN and 0.
(flnormalized-fraction-exponent 12345.6789)
⇒ 0.7535204406738282 and 14
(make-flonum 0.7535204406738282 14)
⇒ 12345.6789
[R7RS flonum]
{scheme.flonum}
Returns 0 if x is positive or 0.0,
and 1 if it is negative (including -0.0
).
(flsign-bit +nan.0)
is implementation-dependent.
(Note: flonum?
is built-in; see 数値に関する述語).
[R7RS flonum]
{scheme.flonum}
Flonum specific version of numerical comparison predicates
=
, <
, >
, <=
and >=
, respectively.
Currently these are just an alias of the generic numerical comparison predicates; hence they don’t reject when you pass non-flonum arguments, but doing so is not portable. These are to give compilers hints that you are passing flonums so that it can optimize.
Note that -0.0
and 0.0
are the same in terms of
these predicates, e.g. (fl<? -0.0 0.0)
is #f
.
If a NaN is passed to any of the arguments, these predicates returns
#f
.
[R7RS flonum]
{scheme.flonum}
Returns #t
iff at least one of x or y is
a NaN.
[R7RS flonum]
{scheme.flonum}
Returns #t
if x is an integral flonum,
#f otherwise.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of zero?
, positive?
and negative?
.
Note that (flnegative? -0.0)
is #f
.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of odd?
and even?
. An error is thrown
if x is not an integer.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of finite?
, infinite?
and
nan?
(see 数値に関する述語).
[R7RS flonum]
{scheme.flonum}
Returns #t
iff x is a normalized/denormalized flonum,
respectively.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of min
and max
, except that
these can take no arguments, in which case -inf.0
and +inf.0
are returned, respectively.
Note that we don’t check whether the arguments are flonums or not, but passing non-flonums are not portable.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of +.
and *.
(see 数値の演算).
Note that we don’t check whether the arguments are flonums or not, but passing non-flonums is not portable.
[R7RS flonum]
{scheme.flonum}
Returns (+ (* x y) z)
, but (potentially) faster, and more accurately.
It calls C99 fma
function internally. (“More accurately” means
that calculation is done in a single step, with rounding once at the end.)
Note: As of release of 0.9.8, MinGW imlementation of fma
seems
to produce slightly off value occasionally, as if it is calculated
with the two steps (rounding after multiplication).
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of -.
and /.
(see 数値の演算).
Note that we don’t check whether the arguments are flonums or not, but passing non-flonums is not portable.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of abs
(see 数値の演算).
Note that we don’t check whether the argument is a flonum or not, but passing non-flonum is not portable.
[R7RS flonum]
{scheme.flonum}
Returns (abs (- x y))
.
Note that we don’t check whether the arguments are flonums or not, but passing non-flonum is not portable.
[R7RS flonum]
{scheme.flonum}
Returns (max (- x y) 0)
.
Note that we don’t check whether the arguments are flonums or not, but passing non-flonum is not portable.
[R7RS flonum]
{scheme.flonum}
Returns 1.0
if x’s sign bit is 0 (zero or positive),
-1.0
if it’s 1 (negative). Same as (flcopysign 1.0 x)
.
Note that (flsgn 0.0)
is 1.0
, while
(flsgn -0.0)
is -1.0
. The result of
passing +nan.0
is implementation-dependent, reflecting
the sign bit of underlying representation; it returns either 1.0
or
-1.0
but you can’t count on which.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of numerator
and denominator
(see 数値の演算).
For infinity and zero, denominator is 1.0.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of floor
, ceiling
,
round
and truncate
(see 数値の演算).
Note that we don’t check whether the arguments are flonums or not, but passing non-flonum is not portable.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of exp
(see 数値の演算).
Returns (expt fl-e x)
.
[R7RS flonum]
{scheme.flonum}
Returns (expt 2 x)
[R7RS flonum]
{scheme.flonum}
Returns (- 1 (expt fl-e x))
, but is much more accureate
when x is small. We all C’s expm1
internally.
[R7RS flonum]
{scheme.flonum}
Returns (*. x x)
(see 数値の演算)
[R7RS flonum]
{scheme.flonum}
Flonum-speicific verison of sqrt
.
[R7RS flonum] {scheme.flonum} Returns cubic root of a flonum x.
[R7RS flonum]
{scheme.flonum}
Calculates (sqrt (* x x) (* y y))
, with avoiding
overflow or underflow during the intermediate steps.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of expt
(see 数値の演算).
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of log
(see 数値の演算).
[R7RS flonum]
{scheme.flonum}
Returns (log (+ x 1))
, but is more accurate than log
when x
is near zero.
[R7RS flonum] {scheme.flonum} Returns base-2 and base-10 logarithm of a flonum x, respectively.
[R7RS flonum] {scheme.flonum} Returns a procedure that calculates base-x logarithm. An error is signalled if x isn’t a rela number greater than 1.0.
[R7RS flonum]
{scheme.flonum}
Flonum-specific version of sin
, cos
, tan
,
asin
, acos
, sinh
, cosh
, tanh
,
asinh
, acosh
, and atanh
, respectively
(see 数値の演算).
Note that flatan
can take one or two arguments; if two arguments
are passed, it calculates (atan (/ y x)
.
[R7RS flonum]
{scheme.flonum}
Returns (fltruncate (fl/ x y))
.
The result is always integer flonum, but x and y doesn’t
need to be integers.
(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}
Returns (- 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} Returns two values:
x
/y
.
(Same as (flremainder x y)
, but we calculate it in different
routines so the result may differ in a few ulp.
This corresponds to C99’s remquo.
This function is useful to reduce the input for the periodic functions with symmetries.
[R7RS flonum] {scheme.flonum} Computes the value of gamma function for x. When x is integer, it is the same as the factorial of x-1.
This is same as Gauche’s built-in gamma
(see 数値の演算).
[R7RS flonum]
{scheme.flonum}
Returns two values, (log (abs (flgamma x)))
and the sign of
(flgamma x)
as 1.0 if it is postiive and -1.0 if it is negative.
The first value is calculated by Gauche’s built-in lgamma
(see 数値の演算). It’s more accurate than using gamma
then
calling log
. The second value is +nan.0
when
x is -inf.0
or +nan.0
.
[R7RS flonum] {scheme.flonum} Returns the n-th order Bessel fucntion of the first kind.
[R7RS flonum] {scheme.flonum} Returns the n-th order Bessel fucntion of the second kind.
[R7RS flonum] {scheme.flonum} Returns the error function erf(x).
[R7RS flonum] {scheme.flonum} Returns the complementary error function, 1 - erf(x).
Previous: R7RS fixnum, Up: R7RS large [Contents][Index]