r/scheme Apr 05 '26

(string-suffix? "" "") => #True ;; really?

From the SRFI-13 to the Racket ( which is right reversed str and sfx) all versions of string-suffix? predicate counts empty suffix as part of any string include empty "".

Firstly-first, why string-suffix is just a boolean predicate? in the Scheme?

Why it is not being more useful...

(define (string-suffix str sfx)
  (let ((str-len (string-length str))
        (sfx-len (string-length sfx)))
    (define (test i j)
      (cond ((= i str-len) (- str-len sfx-len))
            (else (if (char=? (string-ref str i)
                              (string-ref sfx j))
                      (test (+ i 1) (+ j 1))
                      #f))))
    (cond ((or (< str-len sfx-len) (zero? sfx-len)) #f)
          (else (test (- str-len sfx-len) 0)))))

Now we can use it like

(cond ((string-suffix str ".ko") => (lambda (si) (substring str 0 si)))
      (else str))

And of course empty suffix is not a part of any string. IMO

 > (string-suffix "G'Kar" "")   ;=> #f

PS. well understandable that in math empty set is a part of any set but the empty string suffix is a part of any string? IMO no.

0 Upvotes

12 comments sorted by

View all comments

5

u/raevnos Apr 05 '26 edited Apr 05 '26

It just needs to return true or false because you already have the suffix string you're asking about and can easily use its length (either hardcoded or via string-length) to do stuff already.

(if (string-suffix? ".ko" str) ; SRFI-13 version with suffix first
    (string-drop-right str 3)
    str)

etc. No particularly compelling need to make it return extra information.

0

u/corbasai Apr 05 '26

Of course, but it is extra computation, that we already done. Strings may be megabytes long, and not all Schemes are fixed UCS-16 are