Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string-ref function doesn't work as should

Tags:

string

racket

In my code i am trying to check if a string have a specific prefix, to do so,I am using the equal? and string-ref function. but it doesn't seem to work as expected

here is the part I'm talking about:

(: plPrefixContained : String  -> Boolean)
(define (plPrefixContained x )
   (equal? (string-ref x 0) "p"))

(test (plPrefixContained "pcenuc") => true)

I was checking this specific function, that should return true, but I keep getting false for the test. I was trying to change "p" to #/p" and I was trying to use string=? and eq? insted of equal? but nothing.

Any help would be appreciated

(test (plPrefixContained "pcenuc") => true)

I'm using DRracket and the language is #lang pl

like image 965
hyugu Avatar asked May 31 '26 22:05

hyugu


1 Answers

Remember that string-ref returns a character, not a string. For the comparison to succeed, use #\p instead of "p".

(define (plPrefixContained x )
   (equal? (string-ref x 0) #\p))

The above will work. But to make it more explicit, you should use char=? instead of equal?, in this way you'll remember that the comparison is between characters.

like image 110
Óscar López Avatar answered Jun 03 '26 15:06

Óscar López