For Gauche 0.9.8


Previous: , Up: ライブラリモジュール - R7RS標準ライブラリ   [Contents][Index]

10.3 R7RS large

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

以下のライブラリがサポートされているものです:


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.1 scheme.list - R7RSリスト

Module: scheme.list

このモジュールは、豊富なリスト操作手続きのコレクションです (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

List constructors

Function: xcons cd ca

[R7RS list] {scheme.list} (cons ca cd) と同等です。高階手続きへ渡すのに便利です。

Function: list-tabulate n init-proc

[R7RS list] {scheme.list} n個の要素をもつリストを構築し、それぞれの要素を (init-proc i) で生成します。

(list-tabulate 4 values) ⇒ (0 1 2 3)
Function: circular-list elt1 elt2 …

[R7RS list] {scheme.list} 指定した要素をもつ循環リストを構築します。

(circular-list 'z 'q) ⇒ (z q z q z q …)

List predicates

Function: not-pair? x

[R7RS list] {scheme.list} (lambda (x) (not (pair? x)))と同じです。

SRFI-1 では、「真性リストおよびドットリストの両方で、すべての有限リストを 扱う手続き用の終端条件として便利なように用意した」とあります。

Function: list= elt= list …

[R7RS list] {scheme.list} elt= を用いて、n番目の要素をそれぞれ比較することで、 与えられたリストの同値性を決定します。

list= を真性リスト以外に適用するとエラーになります。

同値性判定の手続きは eq? と整合性がなければなりません。すなわち

(eq? x y) ⇒ (elt= x y).

List selectors

Function: first pair
Function: second pair
Function: third pair
Function: fourth pair
Function: fifth pair
Function: sixth pair
Function: seventh pair
Function: eighth pair
Function: ninth pair
Function: tenth pair

[SRFI-1] {scheme.list} リスト(非真性でも可)のn番目の要素を返します。

Function: car+cdr pair

[R7RS list] {scheme.list} (car pair) および (cdr pair) の二つの値を返します。

List miscellaneous routines

Function: zip clist1 clist2 …

[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))
Function: unzip1 list
Function: unzip2 list
Function: unzip3 list
Function: unzip4 list
Function: unzip5 list

[R7RS list] {scheme.list} unzip1 はリストのリストを引数としてとります。それぞれの リストは少くとも一つの要素を含むものでなくてはなりません。結果として それぞれのリストの最初の要素のリストを返します。 unzip2 はリストのリストを引数としてとります。それぞれのリストは 少くとも二つの要素を含むものでなくてはなりません。結果として二つの値を 返します。最初の要素のリストと二番目の要素のリストです。unzip3 は 3番目までの要素について同様です。以下も同様です。

(unzip2 '((1 one) (2 two) (3 three))) ⇒
   (1 2 3) and
   (one two three)

List fold, unfold & map

Function: pair-fold kons knil clist1 clist2 …
Function: pair-fold-right kons knil clist1 clist2 …

[R7RS list] {scheme.list} fold および fold-right と同様ですが、kons 手続き は与えられた clistcar ではなく、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))
Function: unfold p f g seed :optional tail-gen

[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)
Function: unfold-right p f g seed :optional tail

[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)
Function: map! f clist1 clist2 …

[R7RS list] {scheme.list} 手続き fclist1 の各要素と clist2 の対応する要素 に適用され、結果はひとつのリストになります。clist1 のセルは 結果のリストを構築するのに再利用されます。

Function: map-in-order f clist1 clist2 …

[R7RS list] {scheme.list} map の変形バージョンですが、f の適用順序が、引数として 与えられたリストの要素の左から右への順であることを保証します。 Gauche では map の実装はこの順になっているので、map と 同意です。

Function: pair-for-each f clist1 clist2 …

[R7RS list] {scheme.list} for-each と似ていますが、手続き f はまず clist自体に 適用され、次ににそれらのcdr に適用され、となります。

(pair-for-each write '(a b c))
 ⇒ prints (a b c)(b c)(c)

List partitioning

Function: partition pred list
Function: partition! pred list

[R7RS list] {scheme.list} filterremove を同時に行い、 2つのリストを返します。一つ目は pred により list の要素をフィルタリング した結果で、二つ目は pred により list の要素を削除した結果です。

(partition odd? '(3 1 4 5 9 2 6))
  ⇒ (3 1 5 9) (4 2 6)

partition! はその場で更新されるバージョンです。結果を生成するために list を破壊的に更新するかもしれません。

List searching

Function: take-while pred clist
Function: take-while! pred list

[R7RS list] {scheme.list} clist の最初から、pred を満足する限りの最長部分要素を返します。

Function: drop-while pred clist

[R7RS list] {scheme.list} clist の最初から、pred を満足する限りの最長部分要素を削除し、 残りを返します。

Function: span pred clist
Function: span! pred list
Function: break pred clist
Function: break! pred list

[R7RS list] {scheme.list} span(values (take-while pred clist) (drop-while pred clist)) と等価です。breakpred の意味を反転します。

Function: list-index pred clist1 clist2 …

[R7RS list] {scheme.list} pred を満足する最も左の要素のインデックスを返します。 predを満たす要素が無い場合は#fを返します。

Association lists

Function: alist-cons key datum alist

[R7RS list] {scheme.list} (cons (cons key datum) alist) を返します。 これは、Gauche の組み込み手続き acons の別名です。

Lists as sets

これらの手続きはリストを集合としてあつかいます。すなわち、どのような 要素があるかは重要ですが、その順序は重要ではありません。

この範疇にあるすべての手続きは、比較手続き elt= を最初の引数として とります。この比較手続きは与えられた二つの集合の要素が等しいかどうかを 判定します。

リストは検索に線形時間を必要とするため、ここに挙げた手続きは 大きな集合を扱うには向いていません。もし対象となる集合が 二桁以上の要素を持つことが分かっているなら、R7RSセットを 参照してください。

集合の要素の組み合せについては 組み合わせ も参照してください。

Function: lset<= elt= list1 …

[R7RS list] {scheme.list} list1 のすべての要素が list2 (以降の集合)に含まれている ときに限り #t を返します。リストが与えられなかった場合 および一つだけしか与えられなかった場合には、#t を返します。

Function: lset= elt= list1 list2 …

[R7RS list] {scheme.list} list1 のすべての要素が list2 に含まれており、かつ、 list2 のすべての要素が list1 に含まれていれば、#t を返します。

(lset= eq? '(b e a) '(a e b) '(e e b a)) ⇒ #t
Function: lset-adjoin elt= list elt …

[R7RS list] {scheme.list} elt … を集合 list にまだなければ、追加します。 (順序はとくに決っていません。)

(lset-adjoin eq? '(a b c) 'a 'e) ⇒ '(e a b c)
Function: lset-union elt= list1 …

[R7RS list] {scheme.list} list1 … の和集合を返します。

Function: lset-intersection elt= list1 list2 …

[R7RS list] {scheme.list} すべての list に含まれる要素の集合を返します。

Function: lset-difference elt= list1 list2 …

[R7RS list] {scheme.list} list1 には含まれていて、list2 には含まれていない要素の集合を 返します。引数が n 個与えられた場合には、差分をとる二項演算が 畳み込まれます。

Function: lset-xor elt= list1 …

[R7RS list] {scheme.list} 与えられた集合の排他的論理和を返します。すなわち、list1 および list2 のどちらか一方にのみ属する要素からなる集合を返します。 引数が n 個の場合には、xor の二項演算が畳み込まれます。

Function: lset-diff+intersection elt= list1 list2 …

[R7RS list] {scheme.list} 与えられた集合の差集合と積集合のふたつの集合を返します。

Function: lset-union! elt= list …
Function: lset-intersection! elt= list1 list2 …
Function: lset-difference! elt= list1 list2 …
Function: lset-xor! elt= list1 …
Function: lset-diff+intersection! elt= list1 list2 …

[R7RS list] {scheme.list} それぞれ対応する手続きのその場で更新するバージョンです。 最初の引数のリストのセルが結果を構築するのに再利用されるかもしれません。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.2 scheme.vector - R7RSベクタ

Module: scheme.vector

このモジュールは豊富なベクタ操作を提供します。

以下の手続きは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-43vector-mapは組み込みのvector-map-with-indexと 同じです)。新しいコードでsrfi-43を使う必要はほとんどないでしょう。

ベクタ構成子

Function: vector-unfold f length seed …

[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))
Function: vector-unfold-right f length seed …

[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))
Function: vector-reverse-copy vec :optional start end

[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)
Function: vector-concatenate list-of-vectors

[R7RS vector] {scheme.vector} Same as (apply vector-append list-of-vectors).

Function: vector-append-subvectors spec …

[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)

ベクタ上の述語

Function: vector-empty? vec

[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.

Function: vector= elt= vec …

[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.

ベクタ上の繰り返し

Function: vector-fold kons knil vec1 vec2 …

[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)
Function: vector-fold-right kons knil vec1 vec2 …

[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)
Function: vector-count pred vec1 vec2 …

[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
Function: vector-cumulate f seed vec

[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")

ベクタ上の検索

Function: vector-index pred vec1 vec2 …
Function: vector-index-right pred vec1 vec2 …

[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.

Function: vector-skip pred vec1 vec2 …
Function: vector-skip-right pred vec1 vec2 …

[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.

Function: vector-binary-search vec value cmp :optional start end

[R7RS+] {scheme.vector} ソートされたベクタvec中から値valueを探し、 見つかればそのインデックスを、見つからなければ#fを返します。

vecの要素とvalueの比較は手続きcmpで行われます。 cmpは二つの引数を取り、最初の引数の方が小さければ負の整数を、 等しければ0を、最初の引数の方が大きければ正の整数を買えす手続きです。

vecの要素はcmpによる比較に従い小さい方から順にソート済みで なければなりません。この手続きはその仮定のもとに、二分探索を行います。

省略可能なstartend引数は、Gauche独自の拡張です。指定された場合、 start番目の要素(含む)からend番目の要素(含まない)までが探索の対象となります。

Function: vector-any pred vec1 vec2 …

[R7RS vector] {scheme.vector} vec1 vec2 …の各ベクタの先頭から順に、 ひとつづつ対応する要素を取って、それにpredを適用してゆきます。 predが真の値を返したら、直ちにその値を返します。

predを満たす要素が見つからなければ、#fが返されます。

ベクタの長さは異なっていても構いません。最も短いベクタの要素を使い切ったところで 探索は打ちきられます。

Function: vector-every pred vec1 vec2 …

[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
Function: vector-partition pred vec

[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

ベクタの変更子

Function: vector-swap! vec i j

[R7RS vector] {scheme.vector} veci番目の要素とj番目の要素を交換します。 戻り値はunspecifiedです。

(rlet1 v (vector 'a 'b 'c 'd 'e)
  (vector-swap! v 0 2))
  ⇒ #(c b a d e)
Function: vector-reverse! vec :optional start end

[R7RS vector] {scheme.vector} vecの要素を逆順に並べ替えます。戻り値は未定義です。 省略可能引数startendは影響が及ぶ範囲を限定します。

(rlet1 v (vector 'a 'b 'c 'd 'e)
  (vector-reverse! v 0 4))
  ⇒ #(d c b a e)
Function: vector-reverse-copy! target tstart source :optional sstart send

[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)

targetsourceに同じベクタを指定しても構いません。 コピー範囲が重なっていても問題なく動作します。

(rlet1 v (vector 'a 'b 'c 'd 'e)
  (vector-reverse-copy! v 1 v 1))
  ⇒ #(a e d c b)
Function: vector-unfold! f rvec start end seeds …
Function: vector-unfold-right! f rvec start end seeds …

[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)

ベクタの変換

Function: reverse-vector->list vec :optional start end

[R7RS vector] {scheme.vector} (reverse (vector->list vec start end))と同じですが、より効率的です。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.3 scheme.sort - R7RSソート

Module: scheme.sort

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.

Function: list-sort elt< lis
Function: list-sort! elt< lis
Function: list-stable-sort elt< lis
Function: list-stable-sort! elt< lis

[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 ソートとマージ.)

Function: list-sorted? elt< lis

[R7RS sort] {scheme.sort} Returns true if the list list is sorted according to the ordering predicate elt<.

See also sorted? in ソートとマージ.

Function: list-merge elt< lis1 lis2
Function: list-merge! elt< lis1 lis2

[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 ソートとマージ.

Function: vector-sort elt< vec :optional start end
Function: vector-stable-sort elt< vec :optional start end

[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 ソートとマージ.

Function: vector-sort! elt< vec :optional start end
Function: vector-stable-sort! elt< vec :optional start end

[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 ソートとマージ.

Function: vector-sorted? elt< vec :optional start end

[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 ソートとマージ.

Function: vector-merge elt< vec1 vec2 :optional start1 end1 start2 end2
Function: vector-merge! elt< rvec vec1 vec2 :optional rstart start1 end1 start2 end2

[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.

Function: list-delete-neighbor-dups elt= lis
Function: list-delete-neighbor-dups! elt= lis
Function: vector-delete-neighbor-dups elt= vec :optional start end
Function: vector-delete-neighbor-dups! elt= vec :optional start end

[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!

Function: vector-select! elt< vec k :optional start end

[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.

Function: vector-separate! elt< vec k :optional start end

[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.

Function: vector-find-median elt< vec knil :optional mean
Function: vector-find-median! elt< vec knil :optional mean

[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).

  1. If vec is empty, knil is returned. This is the only case knil is used.
  2. If vec has odd number of elements, the element falls in the exactly the midpoint when ordered, is returned.
  3. If vec has even number of elements, the two elements closest to the midpoint is chosen and passed to the procedure mean, and its result is returned. The default of mean is an arithmetic mean of numbers.
(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: , Previous: , Up: R7RS large   [Contents][Index]

10.3.4 scheme.set - R7RSセット

Module: scheme.set

セットとバッグは、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

Constructors

Function: set comparator elt …
Function: bag comparator elt …

[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)
Function: set-unfold stop? mapper successor seed comparator
Function: bag-unfold stop? mapper successor seed comparator

[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)

Predicates

Function: set-contains? set obj
Function: bag-contains? bag obj

[R7RS set] {scheme.set} Check if obj is in the set or the bag.

Function: set-empty? set
Function: bag-empty? bag

[R7RS set] {scheme.set} Returns #t iff the given set or bag is empty.

Function: set-disjoint? set1 set2
Function: bag-disjoint? bag1 bag2

[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.

Accessors

Function: set-member set obj default
Function: bag-member bag obj default

[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"
Function: set-element-comparator set
Function: bag-element-comparator bag

[R7RS set] {scheme.set} Returns the comparator used to compare the elements for the set or the bag.

Updaters

Function: set-adjoin set elt …
Function: bag-adjoin bag elt …

[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.

Function: set-replace set elt
Function: bag-replace bag elt

[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")
Function: set-delete set elt …
Function: bag-delete bag elt …

[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.

Function: set-delete-all set elt-list
Function: bag-delete-all bag elt-list

[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.

Function: set-adjoin! set elt …
Function: bag-adjoin! bag elt …
Function: set-replace! set elt
Function: bag-replace! bag elt
Function: set-delete! set elt …
Function: bag-delete! bag elt …
Function: set-delete-all! set elt-list
Function: bag-delete-all! bag 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.

Function: set-search! set elt failure success
Function: bag-search! bag elt failure success

[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.

The whole set

Function: set-size set
Function: bag-size bag

[R7RS set] {scheme.set} Returns the number of items in the set/bag.

Function: set-find pred set failure
Function: bag-find pred bag failure

[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.

Function: set-count pred set
Function: bag-count pred bag

[R7RS set] {scheme.set} Returns the number of items that satisfy pred in the set/bag.

Function: set-any? pred set
Function: bag-any? pred bag

[R7RS set] {scheme.set} Returns true iff any item in the set/bag satisfy pred.

Function: set-every? pred set
Function: bag-every? pred bag

[R7RS set] {scheme.set} Returns true iff every item in the set/bag satisfy pred.

Mapping and folding

Function: set-map comparator proc set
Function: bag-map comparator proc bag

[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.

Function: set-for-each proc set
Function: bag-for-each proc bag

[R7RS set] {scheme.set} Apply proc to each element in the set/bag. The result of proc is ignored. Return value is undefined.

Function: set-fold proc seed set
Function: bag-fold proc seed bag

[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
Function: set-filter pred set
Function: bag-filter pred bag

[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)
Function: set-remove pred set
Function: bag-remove pred bag

[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)
Function: set-partition pred set
Function: bag-partition pred bag

[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)
Function: set-filter! pred set
Function: bag-filter! pred bag
Function: set-remove! pred set
Function: bag-remove! pred bag
Function: set-partition! pred set
Function: bag-partition! pred bag

[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.

Copying and conversion

Function: set-copy set
Function: bag-copy bag

[R7RS set] {scheme.set} Returns a copy of the set/bag.

Function: set->list set
Function: bag->list 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.

Function: list->set comparator elt-list
Function: list->bag comparator elt-list

[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)
Function: list->set! set elt-list
Function: list->bag! bag 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)
Function: bag->set bag
Function: set->bag set

[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.

Function: set->bag! bag set

[R7RS set] {scheme.set} Adds all items in set to bag, and returns bag. Both bag and set must have the same comparator.

Function: bag->alist bag

[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.

Function: alist->bag comparator alist

[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.

Subsets

Function: set=? set1 set2 …
Function: bag=? bag1 bag2 …

[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.

Function: set<? set1 set2 …
Function: bag<? bag1 bag2 …
Function: set>? set1 set2 …
Function: bag>? bag1 bag2 …
Function: set<=? set1 set2 …
Function: bag<=? bag1 bag2 …
Function: set>=? set1 set2 …
Function: bag>=? bag1 bag2 …

[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.

Set theory operations

Function: set-union set1 set2 …
Function: bag-union bag1 bag2 …

[R7RS set] {scheme.set} Returns a newly allocated set or bag which is a union of all the sets/bags.

Function: set-intersection set1 set2 …
Function: bag-intersection bag1 bag2 …

[R7RS set] {scheme.set} Returns a newly allocated set or bag which is an intersection of all the sets/bags.

Function: set-difference set1 set2 …
Function: bag-difference bag1 bag2 …

[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)
Function: set-xor set1 set2
Function: bag-xor bag1 bag2

[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)
Function: set-union! set1 set2 …
Function: bag-union! bag1 bag2 …
Function: set-intersection! set1 set2 …
Function: bag-intersection! bag1 bag2 …
Function: set-difference! set1 set2 …
Function: bag-difference! bag1 bag2 …
Function: set-xor! set1 set2
Function: bag-xor! bag1 bag2

[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.

Bag-specific procedures

Function: bag-sum bag1 bag2 …
Function: bag-sum! bag1 bag2 …

[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)
Function: bag-product n bag
Function: bag-product! n bag

[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)
Function: bag-unique-size bag

[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
Function: bag-element-count bag elt

[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
Function: bag-for-each-unique proc bag

[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.

Function: bag-fold-unique proc seed 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))
Function: bag-increment! bag elt count
Function: bag-decrement! bag elt count

[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.

Comparators

Comparator: set-comparator
Comparator: bag-comparator

[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: , Previous: , Up: R7RS large   [Contents][Index]

10.3.5 scheme.charset - R7RS文字セット

Module: scheme.charset

文字集合ライブラリを実装します。元は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モジュールで 提供される操作も使えます (コレクションフレームワーク参照)。


Next: , Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.1 文字セットの構築子

Function: list->char-set char-list :optional base-cs
Function: list->char-set! char-list base-cs

[R7RS comparator] {scheme.charset} 与えられた文字のリストchar-listから文字セットを構築して返します。 文字セットbase-csが与えられていた場合、返される文字セットは その文字セットにchar-list中の文字を追加したものとなります。 list->char-set!は、結果を格納するためにbase-csを 変更するかもしれません。

Function: string->char-set s :optional base-cs
Function: string->char-set! s base-cs

[R7RS charset] {scheme.charset} 追加する文字をリストでなく文字列sで与えることを除けば list->char-setlist->char-set!と同じです。

Function: char-set-filter pred char-set :optional base-cs
Function: char-set-filter! pred char-set base-cs

[R7RS charset] {scheme.charset} char-set内の文字のうち、(pred c) が真の値を 返すようなcからなる文字セットを作成して返します。 文字セットbase-csが与えられた場合は、その内容が結果に追加されます。 char-set-filter!は、結果を格納するためにbase-csを変更するかも しれません。

Function: ucs-range->char-set lower upper :optional error? base-cs
Function: ucs-range->char-set! lower upper error? base-cs

[R7RS charset] {scheme.charset}

Function: integer-range->char-set lower upper :optional error? base-cs
Function: integer-range->char-set! lower upper error? base-cs

{scheme.charset}

Function: ->char-set x

[R7RS charset] {scheme.charset} 様々な種類のオブジェクトを文字セットに変換する関数です。xに 文字のコレクション、文字セット、あるいは文字を渡すことができます。 引数が文字セットの場合はそれがそのまま返されます。 引数が文字の場合は、その文字のみを含む文字セットが返されます。

註: R7RS (scheme charset)->char-set は文字列、文字セット、文字のいずれかのみを 引数に取ります。Gaucheでは文字の任意のコレクションを取るように拡張しました。


Next: , Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.2 文字セットの比較

Function: char-set= char-set1 …

[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
Function: char-set<= char-set1 …

[R7RS charset] {scheme.charset}

Function: char-set-hash char-set :optional bound

[R7RS charset] {scheme.charset}


Next: , Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.3 文字セットのイテレーション

Function: char-set-cursor char-set

[R7RS charset] {scheme.charset}

Function: char-set-ref char-set cursor

[R7RS charset] {scheme.charset}

Function: char-set-cursor-next char-set cursor

[R7RS charset] {scheme.charset}

Function: end-of-char-set? ccursor

[R7RS charset] {scheme.charset}

Function: char-set-fold kons knil char-set

[R7RS charset] {scheme.charset}

Function: char-set-unfold pred fun gen seed :optional base-char-set
Function: char-set-unfold! pred fun gen seed base-char-set

[R7RS charset] {scheme.charset}

Function: char-set-for-each proc char-set

[R7RS charset] {scheme.charset}

Function: char-set-map proc char-set

[R7RS charset] {scheme.charset}


Next: , Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.4 文字セットへの問い合わせ

Function: char-set-every pred char-set
Function: char-set-any pred char-set
Function: char-set-count pred char-set

[R7RS charset] {scheme.charset} これらの手続きは、predchar-setに含まれる各文字に適用します。

char-set-everypred#fを返したら直ちに#fを返します。 predが最後まで#fを返さなかった場合は、最後のpredの返り値が char-set-everyの返り値となります。

char-set-anypredが真の値を返した場合に直ちにその値を返します。 predが最後まで#fを返した場合には#fを返します。

char-set-countpredが真の値を返した回数を返します。

char-setは非常に大きくなる可能性があり (例えばごく小さな文字セットの補集合)、 その場合、これらの手続きは非常に長くかかり得ることに注意してください。

Function: char-set->list char-set
Function: char-set->string char-set

[R7RS charset] {scheme.charset} char-setに含まれる文字をリストあるいは文字列にして返します。 大きな文字セットに適用する場合は注意してください。


Next: , Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.5 文字セットに適用できる代数的関数

Function: char-set-adjoin char-set char1 …
Function: char-set-adjoin! char-set char1 …

[R7RS charset] {scheme.charset} char-setchar1 …を加えた文字集合を返します。

Function: char-set-delete char-set char1 …
Function: char-set-delete! char-set char1 …

[R7RS charset] {scheme.charset}

Function: char-set-union char-set …
Function: char-set-union! char-set1 char-set2 …

[R7RS charset] {scheme.charset}

Function: char-set-intersection char-set …
Function: char-set-intersection! char-set1 char-set2 …

[R7RS charset] {scheme.charset}

Function: char-set-difference char-set1 char-set2 …
Function: char-set-difference! char-set1 char-set2 …

[R7RS charset] {scheme.charset}

Function: char-set-xor char-set …
Function: char-set-xor! char-set1 char-set2 …

[R7RS charset] {scheme.charset}

Function: char-set-diff+intersection char-set1 char-set2 …
Function: char-set-diff+intersection! char-set1 char-set2 char-set3 …

[R7RS charset] {scheme.charset}


Previous: , Up: R7RS文字セット   [Contents][Index]

10.3.5.6 定義済みの文字セット

Variable: char-set:letter

[R7RS charset] {scheme.charset}

Variable: char-set:blank

[R7RS charset] {scheme.charset}

Variable: char-set:iso-control

[R7RS charset] {scheme.charset}

Variable: char-set:digit
Variable: char-set:hex-digit

[R7RS charset] {scheme.charset}

Variable: char-set:graphic

[R7RS charset] {scheme.charset}

Variable: char-set:lower-case
Variable: char-set:upper-case
Variable: char-set:title-case

[R7RS charset] {scheme.charset}

Variable: char-set:printing

[R7RS charset] {scheme.charset}

Variable: char-set:punctuation

[R7RS charset] {scheme.charset}

Variable: char-set:whitespace

[R7RS charset] {scheme.charset}

Variable: char-set:symbol

[R7RS charset] {scheme.charset}

Variable: char-set:ascii

[R7RS charset] {scheme.charset}

Variable: char-set:empty

[R7RS charset] {scheme.charset}

Variable: char-set:full

[R7RS charset] {scheme.charset}


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.6 scheme.hash-table - R7RSハッシュテーブル

Module: scheme.hash-table

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!
Function: make-hash-table comparator arg …
Function: make-hash-table equal-proc hash-proc arg …

[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.

Function: hash-table cmpr key value …

[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.

Function: alist->hash-table alist cmpr arg …
Function: alist->hash-table equal-proc hash-proc cmpr arg …

[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.

Function: hash-table-delete! ht key …

[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.

Function: hash-table-intern! ht key failure

[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.

Function: hash-table-update! ht key updater :optional failure success

[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)))
Function: hash-table-pop! ht

[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.

Function: hash-table-find proc ht failure

[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.

Function: hash-table-count ht

[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.

Function: hash-table-map proc cmpr ht

[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.

Function: hash-table-map! proc 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.

Function: hash-table-map->list proc ht

[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.

Function: hash-table-for-each proc ht
Function: hash-table-for-each ht proc

[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).

Function: hash-table-fold proc seed ht
Function: hash-table-fold ht proc seed

[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.

Function: hash-table-prune! proc ht

[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.

Function: hash-table-merge! ht1 ht2

[R7RS hash-table] {scheme.hash-table} This is the same as hash-table-union!, and provided just for the compatibility with srfi-69. Deprecated.

Function: hash obj :optional ignore
Function: string-hash obj :optional ignore
Function: string-ci-hash obj :optional ignore
Function: hash-by-identity obj :optional ignore

[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.

Function: hash-table-equivalence-function ht
Function: hash-table-hash-function ht

[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: , Previous: , Up: R7RS large   [Contents][Index]

10.3.7 scheme.ideque - R7RS変更不可な両端キュー

Module: scheme.ideque

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 変更不可な両端キュー.

Function: ideque element …

[R7RS ideque] {scheme.ideque} Returns an ideque with the given elements.

Function: ideque-unfold p f g seed

[R7RS ideque] {scheme.ideque}

Function: ideque-unfold-right p f g seed

[R7RS ideque] {scheme.ideque}

Function: ideque-tabulate size init

[R7RS ideque] {scheme.ideque}

Function: ideque? idq

[R7RS ideque] {scheme.ideque}

Function: ideque-empty? idq

[R7RS ideque] {scheme.ideque}

Function: ideque-add-front idq x
Function: ideque-add-back idq x

[R7RS ideque] {scheme.ideque}

Function: ideque-front idq
Function: ideque-back idq

[R7RS ideque] {scheme.ideque}

Function: ideque-remove-front idq
Function: ideque-remove-back idq

[R7RS ideque] {scheme.ideque}

Function: ideque-reverse idq

[R7RS ideque] {scheme.ideque}

Function: ideque= idq idq2 …

[R7RS ideque] {scheme.ideque}

Function: ideque-ref idq n

[R7RS ideque] {scheme.ideque}

Function: ideque-take idq n
Function: ideque-take-right idq n

[R7RS ideque] {scheme.ideque}

Function: ideque-drop idq n
Function: ideque-drop-right idq n

[R7RS ideque] {scheme.ideque}

Function: ideque-split-at idq n

[R7RS ideque] {scheme.ideque}

Function: ideque-length idq

[R7RS ideque] {scheme.ideque}

Function: ideque-append idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-zip idq idq2 …

[R7RS ideque] {scheme.ideque}

Function: ideque-map proc idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-filter-map proc idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-for-each proc idq …
Function: ideque-for-each-right proc idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-fold proc knil idq …
Function: ideque-fold-right proc knil idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-append-map proc idq …

[R7RS ideque] {scheme.ideque}

Function: ideque-filter pred idq
Function: ideque-remove pred idq

[R7RS ideque] {scheme.ideque}

Function: ideque-partition pred idq

[R7RS ideque] {scheme.ideque}

Function: ideque-find pred idq :optional failure
Function: ideque-find-right pred idq :optional failure

[R7RS ideque] {scheme.ideque}

Function: ideque-take-while pred idq
Function: ideque-take-while-right pred idq

[R7RS ideque] {scheme.ideque}

Function: ideque-drop-while pred idq
Function: ideque-drop-while-right pred idq

[R7RS ideque] {scheme.ideque}

Function: ideque-span pred idq
Function: ideque-break pred idq

[R7RS ideque] {scheme.ideque}

Function: ideque-any pred idq …
Function: ideque-every pred idq …

[R7RS ideque] {scheme.ideque}

Function: ideque->list idq
Function: list->ideque list

[R7RS ideque] {scheme.ideque}

Function: ideque->generator idq
Function: generator->ideque gen

[R7RS ideque] {scheme.ideque}


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.8 scheme.generator - R7RSジェネレータ

Module: scheme.generator

このモジュールは、ジェネレータとアキュムレータを提供します。 これらは最初に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のセクションで 解説してあります(ジェネレータ参照)。

以下はアキュムレータ手続きです。

Function: make-accumulator kons knil finalizer

[R7RS generator] {scheme.generator} 内部状態をひとつ持つアキュムレータを作って返します。 状態はkhilで初期化されます。 EOFでない値vがアキュムレータに与えられる度に、手続きkons(kons v state)と呼ばれ、その戻り値が新たな状態となります。 EOFがアキュムレータに渡されると、(finalizer state)が呼ばれ、 その結果がアキュムレータの戻り値となります。

Function: list-accumulator
Function: reverse-list-accumulator

[R7RS generator] {scheme.generator} 蓄積された値をリストとして返すアキュムレータを作って返します。 list-accumulatorではリストは与えられた順に、 reverse-list-accumulatorでは逆順になります。

Function: vector-accumulator
Function: reverse-vector-accumulator
Function: bytevector-accumulator

[R7RS generator] {scheme.generator} 蓄積された値をベクタあるいはバイトベクタ(u8vector)として返すアキュムレータを 作って返します。vector-accumulatorbytevector-accumulatorでは値は先頭から順に、 reverse-vector-accumulatorではベクタの最後から逆順に詰められます。 reverse-bytevector-accumulatorはありません。

Function: vector-accumulator! vec at
Function: bytevector-accumulator! bvec at

[R7RS generator] {scheme.generator} vecbvecはそれぞれ変更可能なベクタとバイトベクタ(u8vector)です。 この引数はバッファとして使われます。

与えられた値を、バッファのインデックスatから順に格納してゆくアキュムレータを 作って返します。バッファの最後を越えて値を格納しようとした場合はエラーになります。

EOFが与えられたら、vecまたはbvecをそのまま返します。

Function: string-accumulator

[R7RS generator] {scheme.generator} 文字を受け取り、それを蓄積して最後に文字列として返すアキュムレータを生成して返します。

Function: sum-accumulator
Function: product-accumulator
Function: count-accumulator

[R7RS generator] {scheme.generator} スカラー値を生成するアキュムレータを作って返します。

sum-accumulatorproduct-accumulatorで作られた アキュムレータはそれぞれ、 数値を受け取り、それを蓄積された値に加算または乗算してゆきます。 値の初期値はそれぞれ0と1です。

count-accumulatorで作られたアキュムレータは 任意のオブジェクトを受け取ります。 EOFを受け取ったらそれまで受け取ったオブジェクトの数を返します。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.9 scheme.lseq - R7RS遅延シーケンス

Module: scheme.lseq

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.
Function: lseq? x

[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.

Function: lseq=? elt=? lseq1 lseq2

[R7RS lseq] {scheme.lseq} Compare two lseqs element-wise using elt=? and returns #t iff two lseqs are equal.

Function: lseq-car lseq
Function: lseq-first lseq

[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.

Function: lseq-cdr lseq
Function: lseq-rest lseq

[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.

Function: lseq-take lseq k
Function: lseq-drop lseq k

[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.

Function: lseq-realize lseq

[R7RS lseq] {scheme.lseq} Realizes all the elements in lseq, resulting an ordinary list.

Function: lseq->generator lseq

[R7RS lseq] {scheme.lseq} Creates a generator from lseq. In Gauche, this is same as list->generator.

Function: lseq-length lseq

[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.

Function: lseq-append lseq lseq2 …

[R7RS lseq] {scheme.lseq} Append one or more lseqs lazily. This is the same as lappend in Gauche.

Function: lseq-zip lseq lseq2 …

[R7RS lseq] {scheme.lseq} Returns a lazy sequence in which the first element is a list of first elements of lseqs, and so on.

Function: lseq-map proc lseq lseq2 …

[R7RS lseq] {scheme.lseq} Lazy map. The same as Gauche’s lmap. Returns a lazy sequence.

Function: lseq-for-each proc lseq lseq2 …

[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.

Function: lseq-filter pred lseq
Function: lseq-remove pred lseq

[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.

Function: lseq-take-while pred lseq
Function: lseq-drop-while pred lseq

[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.

Function: lseq-find pred lseq
Function: lseq-find-tail pred lseq
Function: lseq-any pred lseq
Function: lseq-every pred lseq
Function: lseq-index pred lseq
Function: lseq-member pred lseq :optional eq
Function: lseq-memq pred lseq
Function: lseq-memv pred 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: , Previous: , Up: R7RS large   [Contents][Index]

10.3.10 scheme.box - R7RSボックス

Module: scheme.box

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:

When you’re writing portable code, be careful not to depend on the equal? behavoir.

Function: box val

[R7RS box] {scheme.box} Returns a fresh box object that contains the value val.

Function: box? obj

[R7RS box] {scheme.box} Returns #t iff obj is a box object.

Function: unbox box

[R7RS box] {scheme.box} Returns box’s content.

Function: set-box! box val

[R7RS box] {scheme.box} Mutate box’s content with val. Returns unspecified value.


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.11 scheme.list-queue - R7RSリストキュー

Module: scheme.list-queue

リストを元にしたキューのライブラリです。 Gaucheはdata.queueモジュールでキューをサポートしています (スレッドセーフなキューも含まれています。詳しくはキューを参照。) このモジュールは、主としてポータブルなコードのために、 data.queue<queue>の上に実装されています。

このモジュールでいうリストキューは<queue>のインスタンスそのものなので、 make-queueで作ったキューをscheme.list-queueのAPIに渡したり、 make-list-queueで作ったキューをGaucheのキューAPIに渡すこともできます。

註: このライブラリのAPIには、性能のために、 キューが使う内部のペアをそのまま返すことを要求されている ものがあります。これらの内部ペアはキューの操作によって破壊的に変更される可能性があります。 ユーザ側でも変更した場合、その後のキューの動作は保証されません。

Function: make-list-queue lis :optional last

[R7RS list-queue] {scheme.list-queue} リストlisの内容を初期値として持つリストキューを作って返します。 Gaucheでは、リストキューは<queue>のインスタンスです(キュー参照)。

lisの実体は作成されたキューの所有物となります。 呼び出し側は、この関数を呼んだ後でlisを変更してはいけません。 また、作成されたキューの操作によってlisの内容は変更されるでしょう。

省略可能なlast引数は、もし与えられる場合は、lisの最後のペアで なければなりません。この引数が渡された場合、make-list-queueは自分で リストの末尾のペアを見つけるかわりに、呼び出し側を信頼してlastを最後の ペアとして保持します。

Function: list-queue elt …

[R7RS list-queue] {scheme.list-queue} elt …を初期内容とするリストキューを作成して返します。 Gaucheでは、リストキューは<queue>のインスタンスです(キュー参照)。

Function: list-queue-copy queue

[R7RS list-queue] {scheme.list-queue} リストキューqueueのコピーを返します。

Function: list-queue-unfold p f g seed :optional 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)
Function: list-queue-unfold-right p f g seed :optional queue

[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)
Function: list-queue? obj

[R7RS list-queue] {scheme.list-queue} queueがリストキューなら#tを、そうでなければ#fを返します。 Gaucheではdata.queueモジュールのqueue?と同じです。

Function: list-queue-empty? queue

[R7RS list-queue] {scheme.list-queue} queueが空なら#tを、そうでなければ#fを返します。 data.queuequeue-empty?と同じです。

Function: list-queue-front queue

[R7RS list-queue] {scheme.list-queue} queueの先頭の要素を返します。queueが空の場合はエラーを報告します。 data.queuequeue-frontと同じです。

Function: list-queue-back queue

[R7RS list-queue] {scheme.list-queue} queueの末尾の要素を返します。queueが空の場合はエラーを報告します。 data.queuequeue-rearと同じです。

Function: list-queue-list queue

[R7RS list-queue] {scheme.list-queue} queueが内部的に保持している要素のリストを返します。 返されたリストは、queueが操作されれば破壊的に変更される可能性があり、 また返されたリストを破壊的に変更した場合はqueueの一貫性が失われます。 この手続きの主な目的は、他のキュー操作を効率よく実装することです。

単にキューの中身にオーバーヘッド無くアクセスしたい場合は、 list-queue-remove-all!が使えないかどうか検討してください。 そちらはキューの中身のリストを直接返すと同時にキュー自体を空にするので、安全です。

Function: list-queue-fist-last queue

[R7RS list-queue] {scheme.list-queue} queueの内部で要素を保持しているリストの先頭と末尾のペアを返します。 キューが空の場合は、二つの空リストを返します。

この手続きも、list-queue-listと同じく、内部のリストを直接返すため、 返されたリストは、queueが操作されれば破壊的に変更される可能性があり、 また返されたリストを破壊的に変更した場合はqueueの一貫性が失われます。 この手続きの主な目的は、他のキュー操作を効率よく実装することであり、 一般的な用途に使うべきではありません。

Function: list-queue-add-front! queue elt

[R7RS list-queue] {scheme.list-queue} eltqueueの先頭に追加します。 data.queue(queue-push! queue elt)と同じです。

Function: list-queue-add-back! queue elt

[R7RS list-queue] {scheme.list-queue} eltqueueの末尾に追加します。 data.queue(enqueue! queue elt)と同じです。

Function: list-queue-remove-front! queue

[R7RS list-queue] {scheme.list-queue} queueの先頭から要素をひとつ取り除き、その取り除かれた要素を返します。 queueが空ならエラーを報告します。 data.queue(dequeue! queue elt)と同じです。

Function: list-queue-remove-back! queue

[R7RS list-queue] {scheme.list-queue} queueの末尾から要素をひとつ取り除き、その取り除かれた要素を返します。 queueが空ならエラーを報告します。 この手続きは、キューの持つ要素数nに対してO(n)の時間がかかります。 もしこの操作を頻繁に必要とするなら、デック(deque, 両端キュー)を使うことを 検討すべきでしょう。(変更不可な両端キューおよび リングバッファ参照。)

Function: list-queue-remove-all! queue

[R7RS list-queue] {scheme.list-queue} queueを空にして、入っていた全ての要素をリストで返します。 リストはコピーされません。つまりO(1)の操作です。 可能なら、list-queue-listよりはこちらを使う方が安全です。 Gaucheでは、これはdata.queuedeque-all!と同じです。

Function: list-queue-set-list! queue lis :optional last

[R7RS list-queue] {scheme.list-queue} queueを変更して、lisの内容がキューの内容になるようにします。 queueの元の内容は捨てられます。省略可能なlast引数が渡される場合、 それはlisの最後のペアでなければなりません。手続きは呼び出し元を信頼して、 lisをスキャンせずにlastを最後のペアと考えることで O(1)操作を実現しています。

この手続きを呼び出した後では、lisqueueに所有され、破壊的変更を 受けます。この後でlisの内容をあてにしたり変更したりしてはいけません。

Function: list-queue-append queue …

[R7RS list-queue] {scheme.list-queue} 与えられたqueueを全部つないだ要素を持つリストキューを 新たに作成して返します。引数のキューは変更されません。 これは要素の総数nについてO(n)の操作となります。

Function: list-queue-append! queue …

[R7RS list-queue] {scheme.list-queue} 与えられたqueueの要素を全てつないだものを要素とするリストキューを 返します。操作によって、queueの内容は破壊されることがあり、 以降それらのキューを使ってはなりません (Gaucheでは、事故を避けるために、全てのqueueを空にしています)。 結果として返されるキューが、引数のどれともeq?になる必要はない、 ということに注意してください。 これはキューの総数mに対してO(m)の操作です(要素の総数ではなく)。

Function: list-queue-concatenate queues

[R7RS list-queue] {scheme.list-queue} (apply list-queue-append queues).

Function: list-queue-map proc queue

[R7RS list-queue] {scheme.list-queue} procqueueの各要素に適用して得られた結果を要素とする 新たなリストキューを返します。

Function: list-queue-map! proc queue

[R7RS list-queue] {scheme.list-queue} queueの各要素を、それにprocを適用して得られた結果に 置き替えます。

Function: list-queue-for-each proc queue

[R7RS list-queue] {scheme.list-queue} procqueueの各要素に適用します。結果は捨てられます。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.12 scheme.comparator - R7RS比較器

Module: scheme.comparator

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: , Previous: , Up: R7RS large   [Contents][Index]

10.3.13 scheme.mapping - R7RSマップ

Module: scheme.mapping
Module: scheme.mapping.hash

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.


Next: , Previous: , Up: R7RSマップ   [Contents][Index]

10.3.13.1 Mappings

Class: <mapping>

{scheme.mapping} On Gauche, this is just an alias of <tree-map>.

Constructors

Function: mapping comparator key value …

[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
Function: mapping-unfold p f g seed comparator

[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)
Function: mapping/ordered comparator key value …

[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.

Function: mapping-unfold/ordered p f g seed comparator

[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.

Predicates

Function: mapping? obj

[R7RS mapping] {scheme.mapping} Returns #t iff obj is a mapping object.

Function: mapping-empty? m

[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 ツリーマップ).

Function: mapping-contains? m key

[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 ツリーマップ).

Function: mapping-disjoint? m1 m2

[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).

Accessors

Function: mapping-ref m key :optional failure success

[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.

Function: mapping-ref/default m key default

[R7RS mapping] {scheme.mapping} Returns the value associated to key from a mapping m. If m doesn’t have key, default is returned.

Function: mapping-key-comparator m

[R7RS mapping] {scheme.mapping} Returns a comparator used to compare keys in a mapping m. See 基本的な比較器, for the details of comparators.

Updaters

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.

Function: mapping-adjoin m arg …
Function: mapping-adjoin! m arg …

[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
Function: mapping-set m arg …
Function: mapping-set! m arg …

[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
Function: mapping-replace m key value
Function: mapping-replace! m key value

[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
Function: mapping-delete m key …
Function: mapping-delete! m key …

[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.

Function: mapping-delete-all m key-list
Function: mapping-delete-all! m key-list

[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.

Function: mapping-intern m key make-value
Function: mapping-intern! m key make-value

[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
Function: mapping-update m key updater :optional failure success
Function: mapping-update! m key updater :optional failure success

[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
Function: mapping-update/default m key updater default
Function: mapping-update!/default m key updater default

[R7RS mapping] {scheme.mapping}

Function: mapping-pop m :optional failure
Function: mapping-pop! m :optional failure

[R7RS mapping] {scheme.mapping}

Function: mapping-search m k failure success
Function: mapping-search! m k failure success

[R7RS mapping] {scheme.mapping}

The whole mapping

Function: mapping-size m

[R7RS mapping] {scheme.mapping}

Function: mapping-find pred m failure

[R7RS mapping] {scheme.mapping}

Function: mapping-count pred m

[R7RS mapping] {scheme.mapping}

Function: mapping-any? pred m
Function: mapping-every? pred m

[R7RS mapping] {scheme.mapping}

Function: mapping-keys m
Function: mapping-values m

[R7RS mapping] {scheme.mapping}

Function: mapping-entries m

[R7RS mapping] {scheme.mapping}

Mapping and folding

Function: mapping-map proc comparator m

[R7RS mapping] {scheme.mapping}

Function: mapping-map/monotone proc comparator m
Function: mapping-map/monotone! proc comparator m

[R7RS mapping] {scheme.mapping}

Function: mapping-for-each proc m

[R7RS mapping] {scheme.mapping}

Function: mapping-fold kons knil m

[R7RS mapping] {scheme.mapping}

Function: mapping-fold/reverse kons knil m

[R7RS mapping] {scheme.mapping}

Function: mapping-map->list proc m

[R7RS mapping] {scheme.mapping}

Function: mapping-filter pred m
Function: mapping-filter! pred m

[R7RS mapping] {scheme.mapping}

Function: mapping-remove pred m
Function: mapping-remove! pred m

[R7RS mapping] {scheme.mapping}

Function: mapping-partition pred m
Function: mapping-partition! pred m

[R7RS mapping] {scheme.mapping}

Copying and conversion

Function: mapping-copy m

[R7RS mapping] {scheme.mapping}

Function: mapping->alist m

[R7RS mapping] {scheme.mapping}

Function: alist->mapping comparator alist

[R7RS mapping] {scheme.mapping}

Function: alist->mapping! m alist

[R7RS mapping] {scheme.mapping}

Function: alist->mapping/ordered comparator alist
Function: alist->mapping/ordered! m alist

[R7RS mapping] {scheme.mapping}

Submappings

Function: mapping=? comparator m1 m2 …

[R7RS mapping] {scheme.mapping}

Function: mapping<? comparator m1 m2 …
Function: mapping<=? comparator m1 m2 …
Function: mapping>? comparator m1 m2 …
Function: mapping>=? comparator m1 m2 …

[R7RS mapping] {scheme.mapping}

Set operations

Function: mapping-union m1 m2 …
Function: mapping-union! m1 m2 …

[R7RS mapping] {scheme.mapping}

Function: mapping-intersection m1 m2 …
Function: mapping-intersection! m1 m2 …

[R7RS mapping] {scheme.mapping}

Function: mapping-difference m1 m2 …
Function: mapping-difference! m1 m2 …

[R7RS mapping] {scheme.mapping}

Function: mapping-xor m1 m2 …
Function: mapping-xor! m1 m2 …

[R7RS mapping] {scheme.mapping}

Mappings with ordered keys

Function: mapping-min-key m
Function: mapping-max-key m

[R7RS mapping] {scheme.mapping}

Function: mapping-min-value m
Function: mapping-max-value m

[R7RS mapping] {scheme.mapping}

Function: mapping-min-entry m
Function: mapping-max-entry m

[R7RS mapping] {scheme.mapping}

Function: mapping-key-predecessor m obj failure
Function: mapping-key-successor m obj failure

[R7RS mapping] {scheme.mapping}

Function: mapping-range= m obj
Function: mapping-range< m obj
Function: mapping-range<= m obj
Function: mapping-range> m obj
Function: mapping-range>= m obj

[R7RS mapping] {scheme.mapping}

Function: mapping-range=! m obj
Function: mapping-range<! m obj
Function: mapping-range<=! m obj
Function: mapping-range>! m obj
Function: mapping-range>=! m obj

[R7RS mapping] {scheme.mapping}

Function: mapping-split m obj
Function: mapping-split! m obj

[R7RS mapping] {scheme.mapping}

Function: mapping-catenate comparator m1 key value m2
Function: mapping-catenate! m1 key value m2

[R7RS mapping] {scheme.mapping}

Comparators

Function: make-mapping-comparator comparator

[R7RS mapping] {scheme.mapping}

Variable: mapping-comparator

[R7RS mapping] {scheme.mapping}


Previous: , Up: R7RSマップ   [Contents][Index]

10.3.13.2 Hashmaps

Constructors

Function: hashmap comparator key value …

[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
Function: hashmap-unfold p f g seed comparator

[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)

Predicates

Function: hashmap? obj

[R7RS mapping] {scheme.mapping.hash} Returns #t iff obj is a hashmap object.

Function: hashmap-empty? m

[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 ツリーマップ).

Function: hashmap-contains? m key

[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 ツリーマップ).

Function: hashmap-disjoint? m1 m2

[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).

Accessors

Function: hashmap-ref m key :optional failure success

[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.

Function: hashmap-ref/default m key default

[R7RS mapping] {scheme.mapping.hash} Returns the value associated to key from a hashmap m. If m doesn’t have key, default is returned.

Function: hashmap-key-comparator m

[R7RS mapping] {scheme.mapping.hash} Returns a comparator used to compare keys in a hashmap m. See 基本的な比較器, for the details of comparators.

Updaters

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.

Function: hashmap-adjoin m arg …
Function: hashmap-adjoin! m arg …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-set m arg …
Function: hashmap-set! m arg …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-replace m key value
Function: hashmap-replace! m key value

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-delete m key …
Function: hashmap-delete! m key …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-delete-all m key-list
Function: hashmap-delete-all! m key-list

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-intern m key failure
Function: hashmap-intern! m key failure

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-update m key updater :optional failure success
Function: hashmap-update! m key updater :optional failure success

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-update/default m key updater default
Function: hashmap-update!/default m key updater default

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-pop m :optional failure
Function: hashmap-pop! m :optional failure

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-search m k failure success
Function: hashmap-search! m k failure success

[R7RS mapping] {scheme.mapping.hash}

The whole hashmap

Function: hashmap-size m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-find pred m failure

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-count pred m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-any? pred m
Function: hashmap-every? pred m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-keys m
Function: hashmap-values m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-entries m

[R7RS mapping] {scheme.mapping.hash}

Mapping and folding

Function: hashmap-map proc comparator m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-for-each proc m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-fold kons knil m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-map->list proc m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-filter pred m
Function: hashmap-filter! pred m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-remove pred m
Function: hashmap-remove! pred m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-partition pred m
Function: hashmap-partition! pred m

[R7RS mapping] {scheme.mapping.hash}

Copying and conversion

Function: hashmap-copy m

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap->alist m

[R7RS mapping] {scheme.mapping.hash}

Function: alist->hashmap comparator alist

[R7RS mapping] {scheme.mapping.hash}

Function: alist->hashmap! m alist

[R7RS mapping] {scheme.mapping.hash}

Subhashmaps

Function: hashmap=? comparator m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap<? comparator m1 m2 …
Function: hashmap<=? comparator m1 m2 …
Function: hashmap>? comparator m1 m2 …
Function: hashmap>=? comparator m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Set operations

Function: hashmap-union m1 m2 …
Function: hashmap-union! m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-intersection m1 m2 …
Function: hashmap-intersection! m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-difference m1 m2 …
Function: hashmap-difference! m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Function: hashmap-xor m1 m2 …
Function: hashmap-xor! m1 m2 …

[R7RS mapping] {scheme.mapping.hash}

Comparators

Function: make-hashmap-comparator comparator

[R7RS mapping] {scheme.mapping.hash}

Variable: hashmap-comparator

[R7RS mapping] {scheme.mapping.hash}


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.14 scheme.division - R7RS整数除算

Module: scheme.division

このモジュールは、様々な整数除算操作を包括的に提供します。

整数除算の商と剰余には、除数と被除数の符号の考慮によって、 いくつかの定義が考えられます。 Gaucheはそのうちいくつかの定義を組み込みで提供しています: R5RSのquotientremaindermodulo、 R6RSのdivmoddiv0mod0、 そしてR7RSの floor-quotientfloor-remainderfloor/truncate-quotinettruncate-remaindertruncate/、です。

このモジュールは、R7RSの手続きにさらに ceilingroundeuclideanbalancedのバリエーションを加えます。

scheme.divisionの以下の手続きについては組み込みになっているので、 数値の演算を参照してください。

floor-quotient     floor-remainder    floor/
truncate-quotient  truncate-remainder truncate/
Function: ceiling-quotient n d
Function: ceiling-remainder n d
Function: ceiling/ n d

[R7RS division] {scheme.division}

ceiling-quotient = ceiling(n / d)
ceiling-remainder = n - d * ceiling-quotient
ceiling/ = values(ceiling-quotient, ceiling-remainder)
Function: round-quotient n d
Function: round-remainder n d
Function: round/ n d

[R7RS division] {scheme.division}

round-quotient = round(n/d)
round-remainder = n - d * round-quotient
round/ = values(round-quotient, round-remainder)
Function: euclidean-quotient n d
Function: euclidean-remainder n d
Function: euclidean/ n d

[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のdivmodおよびdiv-and-modと同じです。 但し、R6RS版は非整数を引数に取ることもできます(数値の演算参照)。

Function: balanced-quotient n d
Function: balanced-remainder n d
Function: balanced/ n d

[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の div0mod0およびdiv0-and-mod0と同じです。 但し、R6RS版は非整数を引数に取ることもできます(数値の演算参照)。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.15 scheme.bitwise - R7RSビット演算

Module: scheme.bitwise

このモジュールは包括的なビット演算手続きを提供します。 元はsrfi-151で、 ほぼsrfi-60のスーパーセットですが、いくつかの手続きは一貫性と互換性のために 名前が変わりました(srfi-60については整数に対するビット操作参照)。 srfi-60も以前のコードとの互換性のため残されますが、新たに書くコードは このモジュールを使うことを推奨します。

以下の手続きはGauche組み込みになっています。 説明は基本的なビット演算を参照してください。

integer-length    copy-bit          bit-field

基本演算

Function: bitwise-not n

[R7RS bitwise] {scheme.bitwise} nの各ビットを反転したものを返します。 組み込みのlognotと同じです。(基本的なビット演算参照)。

Function: bitwise-and n …
Function: bitwise-ior n …
Function: bitwise-xor n …
Function: bitwise-eqv n …

[R7RS bitwise] {scheme.bitwise} 引数が与えられない場合、これらはそれぞれ-100-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つの手続きはそれぞれ組み込みの logandlogiorlogxorと同じです (基本的なビット演算参照)。

Function: bitwise-nand n0 n1
Function: bitwise-nor n0 n1
Function: bitwise-andc1 n0 n1
Function: bitwise-andc2 n0 n1
Function: bitwise-orc1 n0 n1
Function: bitwise-orc2 n0 n1

[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))

整数演算

Function: arithmetic-shift n count

[R7RS bitwise] {scheme.bitwise} 整数ncountビット左シフトします。countが負ならば、 n-countビット右にシフトされることになります。

これは組み込みのashと同じです(基本的なビット演算参照)。

Function: bit-count n

[R7RS bitwise] {scheme.bitwise} nが正の場合はn中の1であるビットの数を、 nが負の場合はn中の0であるビットの数を返します。

組み込みのlogcountと同じです(基本的なビット演算参照)。

Function: bitwise-if mask n0 n1

[R7RS bitwise] {scheme.bitwise} 整数を返します。戻り値のn番目のビットは、maskn番目のビットが 1であればn0n番目のビット、0であればn1n番目のビット になります。

(bitwise-if #b10101100 #b00110101 #b11001010)
 ⇒ #b01100110

単一ビットの操作

Function: bit-set? index n

[R7RS bitwise] {scheme.bitwise} nのLSBから数えてindex番目(0ベース)のビットが1であれば #tを、0であれば#fを返します。

組み込みのlogbit?と同じです(基本的なビット演算参照)。

Function: bit-swap index1 index2 n

[R7RS bitwise] {scheme.bitwise} nindex1番目のビットとindex2番目のビットを入れ替えた整数を 返します。インデックスは0ベースでLSBから数えます。

Function: any-bit-set? mask n
Function: every-bit-set? mask n

[R7RS bitwise] {scheme.bitwise} n中で、maskでビットの立っている箇所のビットのいずれか(any-bit-set?) あるいはすべて(every-bit-set?)が1であれば#tを返し、 それ以外では#fを返します。

any-bit-set?は組み込みのlogtestとほぼ同じですが、 logtestは可変長引数です (基本的なビット演算参照)。

Function: first-set-bit n

[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と同じです (基本的なビット演算参照)。

ビットフィールド操作

Function: bit-field-any? n start end
Function: bit-field-every? n start end

[R7RS bitwise] {scheme.bitwise} n中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドのうち、一つでもビットが立っていた場合(bit-field-any?)、 あるいは全てのビットが立っていた場合(bit-field-every?)に#tを、 それ以外に#fを返します。

Function: bit-field-clear n start end
Function: bit-field-set n start end

[R7RS bitwise] {scheme.bitwise} n中のstartビット目(含む)からendビット目(含まない)までの ビットを全て0あるいは1にした整数値を返します。

Function: bit-field-replace dst src start end

[R7RS bitwise] {scheme.bitwise} dst中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを、src中の下位(end-start)ビットで置き換えた 値を返します。

(bit-field-replace #b101010 #b010 1 4) ⇒ #b100100

組み込みのcopy-bit-fieldと同じです(基本的なビット演算参照)。

Function: bit-field-replace-same dst src start end

[R7RS bitwise] {scheme.bitwise} dst中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを、 src中の同じ位置にあるビットフィールドに置き換えた値を返します。

(bit-field-replace-same #b111111 #b100100 1 4) ⇒ #b110101
Function: bit-field-rotate n count start end

[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
Function: bit-field-reverse n start end

[R7RS bitwise] {scheme.bitwise} n中のstartビット目(含む)からendビット目(含まない)までの ビットフィールドを逆順にしたものを返します。

(bit-field-reverse #b10100111 0 8)
 ⇒ 229 ; #b11100101

ビット変換

Function: bits->list n :optional len
Function: bits->vector n :optional len

[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という似た手続きがありますが、 ビットの順番が逆です。

Function: list->bits bool-list
Function: vector->bits bool-vector

[R7RS bitwise] {scheme.bitwise} 真偽値のリストまたはベクタを受け取り、 LSBから対応するビットを詰めた正確な整数を返します。 返り値は負になることはありません。

(list->bits '(#f #t #t #t #f #t #t #f #t))
 ⇒ #b101101110

註: srfi-60にはlist->integerという似た手続きがありますが、 ビットの順番が逆です。

Function: bits bool …

[R7RS bitwise] {scheme.bitwise} 真偽値boolの列をLSBから順にビットとして詰めた整数を返します。 返り値は負になることはありません。

(bits #f #t #t #t #f #t #t #f #t)
 ⇒ #b101101110

註: srfi-60にはbooleans->integerという似た手続きがありますが、 ビットの順番が逆です。

Foldとunfoldとgenerate

Function: bitwise-fold kons knil n

[R7RS bitwise] {scheme.bitwise} 整数nのビットを、LSBから(integer-length n)ビット分、順に調べ、 各ビットを真偽値とみなした値とシード値にkonsを適用してゆきます。 konsの戻り値が次のシード値となり、最後のkonsの結果が返されます。

(bitwise-fold cons '() #b10110111)
 ⇒ (#t #f #t #t #f #t #t #t)
Function: bitwise-for-each proc n

[R7RS bitwise] {scheme.bitwise} 整数nの各ビットを真偽値とみなした値に対して、 LSBから順に(integer-length n)ビット分、procを適用してゆきます。 procの結果は捨てられます。

Function: bitwise-unfold p f g seed

[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)
Function: make-bitwise-generator n

[R7RS bitwise] {scheme.bitwise} 整数nの各ビットをLSBから順に真偽値として生成するようなジェネレータを作って返します。 返されるジェネレータは無限です。

これはgauche.integerbits->generatorと似ていますが、 bits->generatorが作るジェネレータの方はninteger-lengthで止まります。 詳しくはジェネレータを参照してください。


Next: , Previous: , Up: R7RS large   [Contents][Index]

10.3.16 scheme.fixnum - R7RS fixnum

Module: scheme.fixnum

このモジュールはfixnumに特化した操作を提供します。 srfi-143として制定されました。

fixnumは絶対値が小さめの正確な整数で、極めて効率的に処理できます。 Gaucheのfixnumは、64ビット環境では62ビット、32ビット環境では30ビットの幅を持ちます。

このモジュールの手続きはfixnumの範囲でしか動作を定義されていませんが、 それを強制するのではないことに注意してください。fixnumでない引数を 渡したり、結果がfixnumの範囲外になった場合に何が起きるかは 処理系依存とされています。これらの手続きは、プログラマがその意図を コンパイラに伝えて最適化を期待するためのものと考えると良いでしょう。

現在のGaucheのアーキテクチャでは一般的な数値計算が効率よく実行できるようになっているので、 これらの手続きは対応する手続きの単なる別名になっています。 将来は、fixnum特有の最適化を導入するかもしれません。

手続きfixnum?は組み込みなのでここでは説明しません。 数値に関する述語を参照してください。

Variable: fx-width

[R7RS fixnum] {scheme.fixnum} 2^(w-1) - 1から-2^(w-1)までの正確な整数が全てfixnumであるような 最大の正整数wに束縛された変数です。 組み込み手続きfixnum-widthが返す値と同じです。 (数値の演算参照)。

Gaucheでは、これは通常32ビット環境で30、64ビット環境で62です。

Variable: fx-greatest
Variable: fx-least

[R7RS fixnum] {scheme.fixnum} 最大および最小のfixnumに束縛された変数です。これらの値は 組み込み手続きgreatest-fixnumおよびleast-fixnumが 返すものと同じです(数値の演算参照)。

次の表はGaucheでの典型的な値を示します。

Platformfx-greatestfx-least
32bit536,870,911-536,870,912
64bit2,305,843,009,213,693,951-2,305,843,009,213,693,952
Function: fx=? i …
Function: fx<? i …
Function: fx<=? i …
Function: fx>? i …
Function: fx>=? i …

[R7RS fixnum] {scheme.fixnum} これらは、fixnumのみに適用しなければならないことを除き、組み込みの =<<=>>=と同じです。

Function: fxzero? i
Function: fxpositive? i
Function: fxnegative? i
Function: fxodd? i
Function: fxeven? i

[R7RS fixnum] {scheme.fixnum} これらは、fixnumのみに適用しなければならないことを除き、組み込みの zero?positive?negative?odd?even?と同じです。

Function: fxmax i j …
Function: fxmin i j …

[R7RS fixnum] {scheme.fixnum} これらは、fixnumのみに適用しなければならないことを除き、組み込みの maxおよびminと同じです。

Function: fx+ i j
Function: fx- i j
Function: fx* i j

[R7RS fixnum] {scheme.fixnum} これらは、常に2つの引数を取ること、 fixnumのみに適用しなければならないこと、 そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、 組み込みの+-*と同じです。

Function: fxneg i

[R7RS fixnum] {scheme.fixnum} これは、fixnumのみに適用しなければならないこと、 そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、 組み込みの単項-と同じです。

Function: fxquotient i j
Function: fxremainder i j
Function: fxabs i
Function: fxsquare i

[R7RS fixnum] {scheme.fixnum} これらは、fixnumのみに適用しなければならないこと、 そして結果がfixnumの範囲に収まる場合だけに使わなければならないことを除き、 組み込みのquotientremainderabssquareと 同じです。

Function: fxsqrt i

[R7RS fixnum] {scheme.fixnum} これらは、fixnumのみに適用しなければならないことを除き、 exact-integer-sqrtと同じです。sqrtではありません。 数値の演算参照。

Function: fx+/carry i j k
Function: fx-/carry i j k
Function: fx*/carry i j k

[R7RS fixnum] {scheme.fixnum} これらはそれぞれ、まず (+ i j k)(- i j k)(+ (* i j) k)を計算し、 その結果をfixnumの範囲内に収まる剰余Rと、溢れQに分割して 2つの値として返します。 すなわち、(+ (* Q (expt 2 fx-width)) R)が上記計算と一致します。 QRは常にfixnumの範囲内であり、またR- 2^(w-1) <= R < 2^(w-1)を満たします。ただしwfx-widthです。

(fx*/carry 1845917459 19475917581 4735374)
 ⇒ -942551854601421179 and 8

(+ (* 8 (expt 2 fx-width)) -942551854601421179)
 ⇒ 35950936292817682053

(+ (* 1845917459 19475917581) 4735374)
 ⇒ 35950936292817682053

これらは拡張精度整数演算をfixnum手続き上に効率よく実装するためのプリミティブです。 ただ、Gaucheでは必要ならbignumを使えば良いので、これらは単に互換性のために提供しています。

Function: fxnot i
Function: fxand i …
Function: fxior i …
Function: fxxor i …
Function: fxarithmetic-shift i count
Function: fxlength i
Function: fxbit-count i
Function: fxcopy-bit index i boolean
Function: fxbit-set? index i
Function: fxbit-field i start end
Function: fxfirst-set-bit i

[R7RS fixnum] {scheme.fixnum} これらはfixnumのみに適用しなければならないことを除き、 組み込みのlognotlogandlogiorlogxorashinteger-lengthlogcountcopy-bitlogbit?bit-fieldtwos-exponent-factorとそれぞれ同じです。 詳しくは基本的なビット演算参照。

Function: fxif mask i j
Function: fxbit-field-rotate i start end
Function: fxbit-field-rotate i start end

[R7RS fixnum] {scheme.fixnum} これらはfixnumのみに適用しなければならないことを除き、 srfi-60のbitwise-ifrotate-bit-fieldreverse-bit-fieldと同じです。 整数に対するビット操作参照。


Previous: , Up: R7RS large   [Contents][Index]

10.3.17 scheme.flonum - R7RS flonum

Module: scheme.flonum

このモジュールはflonumに特化した操作を提供します。 srfi-144として制定されました。

Gaucheでは、flonumはIEEE754倍精度浮動小数点数です。

このモジュールの手続きはflonumの範囲でしか動作を定義されていませんが、 それを強制するのではないことに注意してください。flonumでない引数を 渡した場合の動作は未定義です。これらの手続きは、プログラマがその意図を コンパイラに伝えて最適化を期待するためのものと考えると良いでしょう。

現在のGaucheのアーキテクチャでは一般的な数値計算が効率よく実行できるようになっているので、 これらの手続きは対応する手続きの単なる別名になっています。 将来は、flonum特有の最適化を導入するかもしれません。

手続きflonum?は組み込みなのでここでは説明しません。 数値に関する述語を参照してください。

定数

We also have a few constants in math.const module (see 定数).

Constant: fl-e

[R7RS flonum] {scheme.flonum} e.

Constant: fl-1/e

[R7RS flonum] {scheme.flonum} (/ e).

Constant: fl-e-2

[R7RS flonum] {scheme.flonum} (square e).

Constant: fl-e-pi/4

[R7RS flonum] {scheme.flonum} (expt e (/ pi 4)).

Constant: fl-log2-e

[R7RS flonum] {scheme.flonum} (log2 e). (Same as (/ (log 2))).

Constant: fl-log10-e

[R7RS flonum] {scheme.flonum} (log10 e). (Same as (/ (log 10))).

Constant: fl-log-2

[R7RS flonum] {scheme.flonum} (log 2).

Constant: fl-1/log-2

[R7RS flonum] {scheme.flonum} (/ (log 2)). (Same as (log2 e)).

Constant: fl-log-3

[R7RS flonum] {scheme.flonum} (log 3).

Constant: fl-log-pi

[R7RS flonum] {scheme.flonum} (log pi).

Constant: fl-log-10

[R7RS flonum] {scheme.flonum} (log 10).

Constant: fl-1/log-10

[R7RS flonum] {scheme.flonum} (/ (log 10)). (Same as (log10 e)).

Constant: fl-pi

[R7RS flonum] {scheme.flonum} pi.

Constant: fl-1/pi

[R7RS flonum] {scheme.flonum} (/ pi).

Constant: fl-2pi

[R7RS flonum] {scheme.flonum} (* 2 pi).

Constant: fl-pi/2

[R7RS flonum] {scheme.flonum} (/ pi 2).

Constant: fl-pi/4

[R7RS flonum] {scheme.flonum} (/ pi 4).

Constant: fl-pi-squared

[R7RS flonum] {scheme.flonum} (square pi).

Constant: fl-degree

[R7RS flonum] {scheme.flonum} (/ pi 180).

Constant: fl-2/pi

[R7RS flonum] {scheme.flonum} (/ 2 pi).

Constant: fl-2/sqrt-pi

[R7RS flonum] {scheme.flonum} (/ 2 (sqrt pi)).

Constant: fl-sqrt-2

(sqrt 2).

Constant: fl-sqrt-3

[R7RS flonum] {scheme.flonum} (sqrt 3).

Constant: fl-sqrt-5

[R7RS flonum] {scheme.flonum} (sqrt 5).

Constant: fl-sqrt-10

[R7RS flonum] {scheme.flonum} (sqrt 10).

Constant: fl-1/sqrt-2

[R7RS flonum] {scheme.flonum} (/ (sqrt 2)).

Constant: fl-cbrt-2

[R7RS flonum] {scheme.flonum} (expt 2 1/3).

Constant: fl-cbrt-3

[R7RS flonum] {scheme.flonum} (expt 3 1/3).

Constant: fl-4thrt-2

[R7RS flonum] {scheme.flonum} (expt 2 1/4).

Constant: fl-phi

[R7RS flonum] {scheme.flonum} (/ (+ 1 (sqrt 5)) 2).

Constant: fl-log-phi

[R7RS flonum] {scheme.flonum} (log fl-phi).

Constant: fl-1/log-phi

[R7RS flonum] {scheme.flonum} (/ (log fl-phi)).

Constant: fl-euler

[R7RS flonum] {scheme.flonum} Euler’s constant.

Constant: fl-e-euler

[R7RS flonum] {scheme.flonum} (exp fl-euler)

Constant: fl-sin-1

[R7RS flonum] {scheme.flonum} (sin 1)

Constant: fl-cos-1

[R7RS flonum] {scheme.flonum} (cos 1)

Constant: fl-gamma-1/2

[R7RS flonum] {scheme.flonum} (gamma 1/2)

Constant: fl-gamma-1/3

[R7RS flonum] {scheme.flonum} (gamma 1/3)

Constant: fl-gamma-2/3

[R7RS flonum] {scheme.flonum} (gamma 2/3)

Constant: fl-gamma-1/3

[R7RS flonum] {scheme.flonum} (gamma 1/3)

Constant: fl-greatest
Constant: fl-least

[R7RS flonum] {scheme.flonum} Bound to the largest/smallest positive finite flonum.

The latter is the same as (flonum-min-denormalized) in Gauche (see 数値の比較).

Constant: fl-epsilon

[R7RS flonum] {scheme.flonum} The same as (flonum-epsilon) in Gauche (see 数値の比較).

Constant: fl-fast-fl+*

[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.

Constant: fl-integer-exponent-zero
Constant: fl-integer-exponent-nan

[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.

コンストラクタ

Function: flonum number

[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.)

Function: fladjacent x y

[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
Function: flcopysign x y

[R7RS flonum] {scheme.flonum} Returns a flonum whose absolute value is (abs x) and whose sign is the sign of y.

Function: make-flonum x n

[R7RS flonum] {scheme.flonum} Returns a flonum (* x (expt 2 n)). It is the same as ldexp (see 数値の変換).

アクセサ

Function: flinteger-fraction x

[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
Function: flexponent x

[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
Function: flinteger-exponent x

[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.

Function: flnormalized-fraction-exponent x

[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
Function: flsign-bit x

[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 数値に関する述語).

Function: fl=? x y z …
Function: fl<? x y z …
Function: fl>? x y z …
Function: fl<=? x y z …
Function: fl>=? x y z …

[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.

Function: flunordered? x y

[R7RS flonum] {scheme.flonum} Returns #t iff at least one of x or y is a NaN.

Function: flinteger? x

[R7RS flonum] {scheme.flonum} Returns #t if x is an integral flonum, #f otherwise.

Function: flzero? x
Function: flpositive? x
Function: flnevative? x

[R7RS flonum] {scheme.flonum} Flonum-specific version of zero?, positive? and negative?. Note that (flnegative? -0.0) is #f.

Function: flodd? x
Function: fleven? x

[R7RS flonum] {scheme.flonum} Flonum-specific version of odd? and even?. An error is thrown if x is not an integer.

Function: flfinite? x
Function: flinfinite? x
Function: flnan? x

[R7RS flonum] {scheme.flonum} Flonum-specific version of finite?, infinite? and nan? (see 数値に関する述語).

Function: flnormalized? x
Function: fldenormalized? x

[R7RS flonum] {scheme.flonum} Returns #t iff x is a normalized/denormalized flonum, respectively.

演算

Function: flmax x …
Function: flmin x …

[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.

Function: fl+ x …
Function: fl* x …

[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.

Function: fl+* x y z

[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).

Function: fl- x y …
Function: fl- x y …

[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.

Function: flabs x

[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.

Function: flabsdiff x y

[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.

Function: flposdiff

[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.

Function: flsgn x

[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.

Function: flnumerator x
Function: fldenominator x

[R7RS flonum] {scheme.flonum} Flonum-specific version of numerator and denominator (see 数値の演算).

For infinity and zero, denominator is 1.0.

Function: flfloor x
Function: flceiling x
Function: flround x
Function: fltruncate x

[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.

指数対数関数

Function: flexp x

[R7RS flonum] {scheme.flonum} Flonum-specific version of exp (see 数値の演算). Returns (expt fl-e x).

Function: flexp2 x

[R7RS flonum] {scheme.flonum} Returns (expt 2 x)

Function: flexp-1 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.

Function: flsquare x

[R7RS flonum] {scheme.flonum} Returns (*. x x) (see 数値の演算)

Function: flsqrt x

[R7RS flonum] {scheme.flonum} Flonum-speicific verison of sqrt.

Function: flcbrt x

[R7RS flonum] {scheme.flonum} Returns cubic root of a flonum x.

Function: flhypot x y

[R7RS flonum] {scheme.flonum} Calculates (sqrt (* x x) (* y y)), with avoiding overflow or underflow during the intermediate steps.

Function: flexpt x y

[R7RS flonum] {scheme.flonum} Flonum-specific version of expt (see 数値の演算).

Function: fllog x

[R7RS flonum] {scheme.flonum} Flonum-specific version of log (see 数値の演算).

Function: fllog1+ x

[R7RS flonum] {scheme.flonum} Returns (log (+ x 1)), but is more accurate than log when x is near zero.

Function: fllog2 x
Function: fllog10 x

[R7RS flonum] {scheme.flonum} Returns base-2 and base-10 logarithm of a flonum x, respectively.

Function: make-fllog-base x

[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.

三角関数

Function: flsin x
Function: flcos x
Function: fltan x
Function: flasin x
Function: flacos x
Function: flatan x
Function: flatan y x
Function: flsinh x
Function: flcosh x
Function: fltanh x
Function: flasinh x
Function: flacosh x
Function: flatanh x

[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).

整数除算

Function: flquotient x y

[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
Function: flremainder x y

[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
Function: flremquo x y

[R7RS flonum] {scheme.flonum} Returns two values:

This corresponds to C99’s remquo.

This function is useful to reduce the input for the periodic functions with symmetries.

特殊関数

Function: flgamma x

[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 数値の演算).

Function: flloggamma x

[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.

Function: flfirst-bessel n x

[R7RS flonum] {scheme.flonum} Returns the n-th order Bessel fucntion of the first kind.

Function: flsecond-bessel n x

[R7RS flonum] {scheme.flonum} Returns the n-th order Bessel fucntion of the second kind.

Function: flerf x

[R7RS flonum] {scheme.flonum} Returns the error function erf(x).

Function: flerfc x

[R7RS flonum] {scheme.flonum} Returns the complementary error function, 1 - erf(x).


Previous: , Up: R7RS large   [Contents][Index]