Так же, как в игре Magic-8-ball, я пытаюсь построить модель, которая может отвечать на различные вопросы в следующем порядке:
- «Это точно»
- «Outlook хороший»
- «Спросите позже»
- «Outlook не очень хороший».
И выдает «Вы не задали вопрос, попробуйте еще раз«если вопрос пустой, обозначается (нет ответа)
Я записал код для этого вопроса, но пустой регистр не работает.
То есть (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))
Тест должен пройти, а мой - нет.Проблема возникает, когда строка пуста.
Может кто-нибудь сказать мне, в чем проблема?Спасибо!