Scheme 关于球拍中的符号
当我输入这些时,我得到了我期望的答案Scheme 关于球拍中的符号,scheme,racket,Scheme,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)
(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)))
但当我想知道学生们是否有相同的名字时,它告诉我,他们期望一个符号作为第一个参数,但给出了John
;;(same-major? student1 student2) -> FALSE
;;(same-mejor? student1 student3) -> True
我做错了什么?
'CS
和'Math
是符号,“John”、“Jane”和“Jim”不是(它们是字符串)。错误消息告诉您,symbol=?
的参数必须是符号
要比较字符串是否相等,可以使用
string=?
或只使用equal?
(这适用于字符串、符号和几乎所有其他内容)。'CS
和'Math
是符号,“John”、“Jane”和“Jim”不是(它们是字符串)。错误消息告诉您,symbol=?
的参数必须是符号
要比较字符串是否相等,可以使用string=?
或仅使用equal?
(这适用于字符串、符号和几乎所有其他内容)。更改此项:
(define (same-first? s1 s2)
(symbol=? (student-first s1)
(student-first s2)))
为此:
(symbol=? (student-major s1)
(student-major s2)))
请注意,您比较的是字符串而不是符号,因此必须使用适当的相等过程。更改此项:
(define (same-first? s1 s2)
(symbol=? (student-first s1)
(student-first s2)))
为此:
(symbol=? (student-major s1)
(student-major s2)))
请注意,您正在比较字符串而不是符号,因此必须使用适当的相等过程