published on in blog

about symbols in racket

(define-struct student (first last major)) (define student1 (make-student "John" "Smith" 'CS)) (define student2 (make-student"Jane" "Jones" 'Math)) (define student3 (make-student "Jim" "Black" 'CS)) #;(define (same-major? s1 s2) (symbol=? (student-major s1) (student-major s2))) 

when I type these in, I get the answer I expect.

;;(same-major? student1 student2) -> FALSE ;;(same-mejor? student1 student3) -> True 

But when I want to find out if the students have the same first name, it tells me that they expect a symbol as a 1st argument, but given John.

(define (same-first? s1 s2) (symbol=? (student-first s1) (student-first s2))) 

What am I doing wrong?

2 Answers

'CS and 'Math are symbols, "John", "Jane" and "Jim" are not (they're strings). As the error message is telling you, the arguments to symbol=? need to be symbols.

To compare strings for equality, you can use string=? or just equal? (which works with strings, symbols and pretty much everything else).

Change this:

(symbol=? (student-major s1) (student-major s2))) 

To this:

(string=? (student-major s1) (student-major s2))) 

Notice that you're comparing strings not symbols, so the appropriate equality procedure must be used.

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaG1tYmx9doWOmpmoraRiwLq5waijrGWZo3qzrcKknK0%3D