魔术八球?在方案中
就像 Magic-8-ball 游戏一样,我试图建立一个模型,可以按照以下顺序回答不同的问题:
- “这是肯定的”
- “前景良好”
- “稍后再问”
- “前景不太好”。
如果问题是空的,则生成“你没有问问题,请重试”,用(无答案)表示
我写下了这个问题的代码,但空的情况不起作用。
也就是说,(magic-8-ball "")
应该生成“您没有问问题,请重试”。
;; 8-ball-answers: (listof string)
(define 8-ball-answers (list "It is certain" "Outlook good" "Ask again later" "Outlook not so good"))
;; no-answer: string
;; Purpose: correct form of string to produce when magic-8-ball consumes empty string
(define no-answer "you did not ask a question, try again")
;;magic-8-ball: string -> string
;;Purpose: consumes a string and produces a string
;;Effects: modifies (8-ball-answers). If the string is empty, produces
;; "you did not ask a question, try again". Otherwise, changes
;; "It is certain" to "Outlook good",
;; "Outlook is good" to "Ask again later",
;; "Ask again later" to "Outlook not so good",
;; and "Outlook not so good" to "It is certain".
(define (magic-8-ball s)
(local
[
;; new-list represents the new value of the list 8-ball-answers
;; after every time the function is called.
(define next-answer (first 8-ball-answers))]
(begin
(cond [(equal? s "") no-answer]
[else (set! 8-ball-answers (append (rest 8-ball-answers)
(list next-answer)))])
next-answer)))
这是我的测试用例:
(check-expect (and (equal? (magic-8-ball "Do you love me?")
"It is certain")
(equal? (magic-8-ball "How is your life?")
"Outlook good")
(equal? (magic-8-ball "")
"you did not ask a question, try again")
(equal? (magic-8-ball "2nd0A-wmQ232.asdA?")
"Ask again later")
(equal? (magic-8-ball "No questions here")
"Outlook not so good")
(equal? (magic-8-ball "Now do you hate me?")
"It is certain"))
true))
测试应该通过,但我的没有。当字符串为空时就会出现问题。
谁能告诉我问题是什么? 谢谢!
Just like the Magic-8-ball game does, I am trying to build a model that can answer different questions in the order of:
- "It is certain"
- "Outlook good"
- "Ask again later"
- "Outlook not so good".
And produces "you did not ask a question, try again" if the question is empty, denoted by (no-answer)
I wrote down the code for this question but the empty case is not working.
That is, (magic-8-ball "")
should produce "you did not ask a question, try again".
;; 8-ball-answers: (listof string)
(define 8-ball-answers (list "It is certain" "Outlook good" "Ask again later" "Outlook not so good"))
;; no-answer: string
;; Purpose: correct form of string to produce when magic-8-ball consumes empty string
(define no-answer "you did not ask a question, try again")
;;magic-8-ball: string -> string
;;Purpose: consumes a string and produces a string
;;Effects: modifies (8-ball-answers). If the string is empty, produces
;; "you did not ask a question, try again". Otherwise, changes
;; "It is certain" to "Outlook good",
;; "Outlook is good" to "Ask again later",
;; "Ask again later" to "Outlook not so good",
;; and "Outlook not so good" to "It is certain".
(define (magic-8-ball s)
(local
[
;; new-list represents the new value of the list 8-ball-answers
;; after every time the function is called.
(define next-answer (first 8-ball-answers))]
(begin
(cond [(equal? s "") no-answer]
[else (set! 8-ball-answers (append (rest 8-ball-answers)
(list next-answer)))])
next-answer)))
Here is my test case:
(check-expect (and (equal? (magic-8-ball "Do you love me?")
"It is certain")
(equal? (magic-8-ball "How is your life?")
"Outlook good")
(equal? (magic-8-ball "")
"you did not ask a question, try again")
(equal? (magic-8-ball "2nd0A-wmQ232.asdA?")
"Ask again later")
(equal? (magic-8-ball "No questions here")
"Outlook not so good")
(equal? (magic-8-ball "Now do you hate me?")
"It is certain"))
true))
The test should pass, but mine did not. The problem occurs when the string is empty.
Can anyone tell me what the problem is?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很好,你修好了空支票。现在我可以运行代码了:)
空支票并不是您唯一的问题。查看以下代码块并告诉我您的期望:
我期望
next-answer
,与条件结果无关。我想您可能希望您的else
条件在循环答案后返回next-answer
。问题是你的开始
。您测试一个条件,然后返回next-answer
。也许您想要类似的东西:如果字符串为空,则返回
no-answer
,否则返回循环的next-answer
。虽然可能是正确的,但我认为这段代码相当丑陋。考虑重构循环并返回下一个答案位。我的结构很糟糕。Good, you fixed your empty check. Now I can run the code :)
The empty check was not your only problem. Look at the following block of code and tell me what you expect:
I expect
next-answer
, independent of the condition results. I imagine that you probably wanted yourelse
condition to returnnext-answer
after cycling the answers. The problem is yourbegin
. You test a condition, then returnnext-answer
. Perhaps you wanted something like:This returns
no-answer
if the string is empty and the cyclednext-answer
otherwise. While probably correct, I think this code is rather ugly. Consider refactoring the cycle-and-return-next-answer bit. The structure is nasty as I have it.