魔术八球?在方案中

发布于 2024-11-15 05:32:54 字数 2207 浏览 1 评论 0原文

就像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2024-11-22 05:32:54

很好,你修好了空支票。现在我可以运行代码了:)

空支票并不是您唯一的问题。查看以下代码块并告诉我您的期望:

(begin
  (cond [(equal? s "") no-answer]
        [else (set! 8-ball-answers (append (rest 8-ball-answers)
                                           (list next-answer)))])
  next-answer)

我期望 next-answer,与条件结果无关。我想您可能希望您的 else 条件在循环答案后返回 next-answer。问题是你的开始。您测试一个条件,然后返回next-answer。也许您想要类似的东西:

(cond [(equal? s "") no-answer]
      [else (begin (set! 8-ball-answers (append (rest 8-ball-answers)
                                                (list 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:

(begin
  (cond [(equal? s "") no-answer]
        [else (set! 8-ball-answers (append (rest 8-ball-answers)
                                           (list next-answer)))])
  next-answer)

I expect next-answer, independent of the condition results. I imagine that you probably wanted your else condition to return next-answer after cycling the answers. The problem is your begin. You test a condition, then return next-answer. Perhaps you wanted something like:

(cond [(equal? s "") no-answer]
      [else (begin (set! 8-ball-answers (append (rest 8-ball-answers)
                                                (list next-answer)))
                   next-answer)])

This returns no-answer if the string is empty and the cycled next-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文