Reputation: 602
(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)))
so I am trying to get a boolean as an output if two students are the same age, but when I run it, it says it expects a string as the 1st argument, but given 19. What is the problem?
Upvotes: 1
Views: 475
Reputation: 235984
A couple of your questions are related, you seem to be struggling with comparisons for different data types, here are some pointers:
=
char=?
symbol=?
string=?
equal?
, the catch-all procedure that will work for several types and will return true as long as both of its operands are of the same type and equalFor example, all of the following comparisons will return #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))
Upvotes: 3
Reputation: 1979
You create students with their age
fields being integers, not strings (note the lack of double-quotation marks), then try to use string=?
function to compare them. You should either use the =
function to compare on 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)))
or create students with their age
fields represented as strings:
(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)))
Upvotes: 1