将数据定义添加到Racket中的结构

时间:2013-01-27 17:53:58

标签: scheme racket

(define-struct animal (name species age breakfast-hour dinner-hour))  
(define-struct attendant (name a1 a2 a3))
(define attendant1 (make-attendant "Dave" 'gorillas 'bats 'mandrills ))
(define attendant2 (make-attendant "John" 'crocodiles 'ocelots 'capybara ))                                  
(define attendant3 (make-attendant "Joe" 'pottos 'tapirs 'vultures ))

我如何向这些添加数据定义?目前给我的所有这些都是“(例如;动物是...)”。

1 个答案:

答案 0 :(得分:4)

首先定义结构:

(define-struct animal (name species age breakfast-hour dinner-hour))
(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 10))
(define crocodile (make-animal "Swampy" "Crocodile" 1 10 18))
(define ocelot (make-animal "Ozzy" "Ocelot" 7 7 17))
(define capybara (make-animal "Capy" "Capybara" 4 6 18))
(define potto (make-animal "Spot" "Potto" 2 2 6))
(define tapir (make-animal "Stripey" "Tapir" 3 10 17))
(define vulture (make-animal "Beaky" "Vulture" 10 9 19))

最后,你创造了服务员,给每个人或者gal给他们相应的动物:

(define attendant1 (make-attendant "Dave" gorilla bat mandrill))
(define attendant2 (make-attendant "John" crocodile ocelot capybara))
(define attendant3 (make-attendant "Joe" potto tapir vulture))

请注意,为了创建服务员,我们必须参考之前定义的动物之一。