(define-struct student (first last major age))
(define student1 (make-student "David" "Smith" 'Math 19))
(define student2 (make-student"Joe" "Jones" 'Math 21))
(define student3 (make-student "Eli" "Black" 'Spanish 20))
(define (same-age? s1 s2)
(string=? (student-age s1)
(student-age s2)))
所以我试图得到一个布尔值作为输出,如果两个学生年龄相同,但是当我运行它时,它说它期望一个字符串作为第一个参数,但是给出19.问题是什么?
答案 0 :(得分:3)
你的几个问题是相关的,你似乎在努力比较不同的数据类型,这里有一些指示:
=
char=?
symbol=?
string=?
equal?
,这个包含多个类型的catch-all过程只要两个操作数的类型相同且相等就会返回true 例如,以下所有比较都将返回#t
:
(equal? 1 1)
(equal? 1.5 1.5)
(equal? #\a #\a)
(equal? 'x 'x)
(equal? "a" "a")
(equal? (list 1 2 3) (list 1 2 3))
答案 1 :(得分:1)
您创建的学生的age
字段为整数,而不是字符串(请注意缺少双引号),然后尝试使用string=?
函数进行比较。您应该使用=
功能在age
上进行比较:
(define-struct student (first last major age))
(define student1 (make-student "David" "Smith" 'Math 19))
(define student2 (make-student "Joe" "Jones" 'Math 21))
(define student3 (make-student "Eli" "Black" 'Spanish 20))
(define (same-age? s1 s2)
(= (student-age s1)
(student-age s2)))
或创建将age
字段表示为字符串的学生:
(define-struct student (first last major age))
(define student1 (make-student "David" "Smith" 'Math "19"))
(define student2 (make-student "Joe" "Jones" 'Math "21"))
(define student3 (make-student "Eli" "Black" 'Spanish "20"))
(define (same-age? s1 s2)
(string=? (student-age s1)
(student-age s2)))