首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开发简单的函数

开发简单的函数
EN

Stack Overflow用户
提问于 2013-01-29 09:42:11
回答 1查看 64关注 0票数 1
代码语言:javascript
复制
(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))

我需要一个函数,它接受一只动物,并返回是否是进餐时间,如果我拿大猩猩,那么晚餐时间应该是10点。这就是我所做的。忽略上面数字上的引号。

代码语言:javascript
复制
(define (meal-time? e1 e2)
  (string=? (animal-species e1)
            (animal-dinnerhour e2)))

它可以运行,但wnt会给我一个输出。有什么理由不给我输出吗?

edit- (meal-time? gorilla 10)告诉我它期望的是一只动物,但给出了10只。

EN

回答 1

Stack Overflow用户

发布于 2013-01-29 10:35:13

您的meal-time?函数接受两个动物作为参数(因为您在两个参数上都使用了animal-访问器函数),但是您使用一个动物和一个数字来调用它。所以你会得到一条错误消息,告诉你第二个参数应该是一个动物。

如果你使用两个动物作为参数来调用你的函数,你将不会再得到错误。你会得到#f的。你的功能是:它检查第一个动物的物种是否等于第二个动物的晚餐时间。因为没有名字是数字的物种,所以这永远不会是真的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14574332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档