Scheme 方案中的数据定义?
我今天一直在研究这个问题,剩下的工作就是开发一个函数,该函数接受服务员并返回服务员分配给的动物的总年龄。所以如果我拿服务员戴夫为例,我会得到10分。不知道从哪里开始。你怎么把年龄加起来Scheme 方案中的数据定义?,scheme,racket,Scheme,Racket,我今天一直在研究这个问题,剩下的工作就是开发一个函数,该函数接受服务员并返回服务员分配给的动物的总年龄。所以如果我拿服务员戴夫为例,我会得到10分。不知道从哪里开始。你怎么把年龄加起来 (define-struct animal (name species age breakfasthour dinnerhour)) (define-struct attendant (name a1 a2 a3)) (define gorilla (make-animal "Koko" "G
(define-struct animal (name species age breakfasthour dinnerhour))
(define-struct attendant (name a1 a2 a3))
(define gorilla (make-animal "Koko" "Gorilla" "4" "8" "10"))
(define bat (make-animal "Bruce" "Bat" "1" "23" "5"))
(define mandrill (make-animal "Manny" "Mandrill" "5" "8" "7"))
(define crocodile (make-animal "Swampy" "Crocodile" "1" "10" "8"))
(define ocelot (make-animal "Ozzy" "Ocelot" "7" "7" "17"))
(define capybara (make-animal "Capy" "Capybara" "4" "6" "8"))
(define potto (make-animal "Spot" "Potto" "2" "2" "6"))
(define tapir (make-animal "Stripey" "Tapir" "3" "10" "6"))
(define vulture (make-animal "Beaky" "Vulture" "10" "9" "6"))
(define attendant1 (make-attendant "Dave" gorilla bat mandrill))
(define attendant2 (make-attendant "John" crocodile ocelot capybara))
(define attendant3 (make-attendant "Joe" potto tapir vulture))
#;(define (meal-time? e1 e2)
(string=? (animal-species e1)
(animal-dinnerhour e2)))
#;(define (animal-template s...)
(...(animal-name s)...
(animal-species s)...
(animal-age s)...
(animal-breakfasthour s)...
(animal-dinnerhour s)...))
#;(define (attendant-template s...)
(...(attendant-name s)...
(attendant-s1 s)...
(attendant-s2 s)...
(attendant-s3 s)...))
只需对每种类型使用访问器过程:
(define (animals-age att)
(+ (animal-age (attendant-a1 att))
(animal-age (attendant-a2 att))
(animal-age (attendant-a3 att))))
很明显,只有当年龄是一个数字时,在当前代码中是一个字符串(为什么?),请考虑将年龄表示为数字,它更有意义。
< P>简单地使用每种类型的访问程序:(define (animals-age att)
(+ (animal-age (attendant-a1 att))
(animal-age (attendant-a2 att))
(animal-age (attendant-a3 att))))
很清楚,只有当年龄是一个数字时,在你当前的代码中是一个字符串(为什么?),请考虑把年龄表示为数字,这就更有意义了。