Using (string-downcase ...) makes (eq? ...) fail

I have these functions here

(define (greetfmt name)
  (string-append "Hello " name "!"))

(define (auth? name)
  (or
   (eq? name "alice")
   (eq? name "bob")))

(define (greet name)
;;(if (auth? name)
  (if (auth? (string-downcase name))
      (greetfmt name)
      "Huh?"))

When I use (string-downcase) the comparison operator fails.

With downcase. This happens for any input:

> (greet "alice")
"Huh?"

Without downcase:

> (greet "alice")
"Hello alice!"
> (greet "grafcube")
"Huh?"

This makes me think that downcase is doing something else to the string but after debugging for a while I haven't figured out what the problem is. Any help is appreciated.

cf. https://docs.racket-lang.org/reference/Equality.html#%28part._model-eq%29 and https://docs.racket-lang.org/reference/strings.html

In particular, you almost certainly want equal? and not eq?.

3 Likes