在Scheme中编写一个typeof过程
帮我回答简单方案中出现的以下问题
6.7 编写程序type-of 接受任何内容作为其参数并返回单词 word、sentence、number 或 boolean 之一:(
> (type-of '(getting better))
SENTENCE
> (type-of 'revolution)
WORD
> (type-of (= 3 3))
BOOLEAN
即使数字是单词,如果其参数是数字,您的过程也应该返回 number。)
Help me answer the following question appears in Simply Scheme
6.7 Write a procedure type-of that takes anything as its argument and returns one of the words word, sentence, number, or boolean:
> (type-of '(getting better))
SENTENCE
> (type-of 'revolution)
WORD
> (type-of (= 3 3))
BOOLEAN
(Even though numbers are words, your procedure should return number if its argument is a number.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
cond
形式检查多个条件并相应地执行操作。您可以使用谓词boolean?
、number?
、word?
和sentence?
来查明是否value 分别是布尔值、数字、单词或句子。这基本上就是全部内容了。您唯一需要考虑的是
number?
的大小写必须位于word?
的大小写之前(因为word?
也会返回正如练习中指出的那样,对于数字来说也是如此)。1 前两个是标准方案,后两个在本书附带的 simple.scm 中定义。
You can use the form
cond
to check several conditions and execute an action accordingly. You can use the predicatesboolean?
,number?
,word?
andsentence?
¹ to find out whether a value is a boolean, number, word or sentence respectively. That's basically all there is to it.The only thing you need to consider is that the case for
number?
must come before the case forword?
(becauseword?
would also return true for numbers as the exercise helpfully points out).¹ The first two are standard scheme, the latter two are defined in simply.scm, which comes with the book.