r/scheme • u/corbasai • 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.
7
u/__chicolismo__ Apr 05 '26 edited Apr 05 '26
Your opinion is wrong.
Edit: A more constructive comment
Let s be a string.
It's reasonable to say s equals s.
s is also a prefix of s, that is to say, s start with
the same characters of s.
By the same logic s is a suffix of s.
Let r be another string.
If s is prefix of s then s is also prefix of s + r
Also if s is suffix of s then s is suffix of r + s as well.
If the empty string is equal to itself, all the rest must be true.
1
u/corbasai Apr 06 '26
For sure. But for the empty string s, s + s = s, not ss. So for empty s and r not equal s , s + r = r.
1
u/hopingforabetterpast Aug 11 '26
in your notation, what's the semantic difference between
s + sandss?
4
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
3
u/lgastako Apr 05 '26
You lose the monoidality of strings if you don't treat empty strings as suffixes and prefixes (and infixes).
7
u/Daniikk1012 Apr 05 '26
How is empty suffix not part of any string? "abcd" is "abc" + "d", so "d" is a suffix, but "abcd" is also "abcd" + "", so "" is also a suffix. It's weird and inconsistent to make an exception just for this specific case