Next: Insertions in immutable ropes, Previous: Balancing a rope, Up: Introduction [Contents][Index]
In order to represent ropes, we use an SRFI-9 record type (actually the Guile extension for immutable records):
(define-immutable-record-type <rope> (make-rope left right length depth) rope? (left rope-left) (right rope-right) (length rope-length') (depth rope-depth'))
Example 1.1: The definition of the <rope> record.
This captures the internal nodes. Leaves are plain strings. The reason we define the getters with apostrophes is because we provide polymorphic replacements ‘rope-length’ and ‘rope-depth’ that also work on strings and return respectively the length of the string and ‘0’. For related reasons we also provide ‘rope’ as a constructor that only accepts two arguments, as we do not want users of the API to misconstruct ropes with incorrect length or depth metadata.
For example, here is the construction of a rope:
(rope "Hear " (rope "the new " "madness!")) ⇒ #<<rope> left: "Hear " right: … length: 21 depth: 2>
As stated the length and depth getters also apply to strings:
(rope-length "GNU Guile") ⇒ 9
This is a lean representation that makes sparing use of records.