魔术八球?在方案中

| 就像Magic-8球游戏一样,我正在尝试构建一个模型,该模型可以按以下顺序回答不同的问题: \“可以肯定的是\” \“前景良好\” \“稍后再询问\\” \\“前景不太好\\”。 如果问题为空,则产生“您没有提出问题,请重试”,表示为(无答案​​) 我为这个问题写下了代码,但是空的情况不起作用。 也就是说,“ 0”应产生“您没有问问题,请重试”。
;; 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))
测试应该通过,但是我没有通过。当字符串为空时,会发生此问题。 谁能告诉我问题是什么? 谢谢!     
已邀请:
        好,您已将空支票固定好。现在我可以运行代码了:) 空支票不是您唯一的问题。查看以下代码块,并告诉我您的期望:
(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
。问题是您的7英镑。您测试一个条件,然后返回
next-answer
。也许您想要这样的东西:
(cond [(equal? s \"\") no-answer]
      [else (begin (set! 8-ball-answers (append (rest 8-ball-answers)
                                                (list next-answer)))
                   next-answer)])
如果字符串为空,则返回“ 10”,否则返回循环的“ 4”。虽然可能正确,但我认为这段代码很难看。考虑重构循环并返回下一个答案位。我的结构很讨厌。     

要回复问题请先登录注册