Josh
Josh

Reputation: 519

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?

Upvotes: 1

Views: 1968

Answers (2)

Óscar López
Óscar López

Reputation: 236140

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.

Upvotes: 1

sepp2k
sepp2k

Reputation: 370425

'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).

Upvotes: 4

Related Questions