【发布时间】:2020-12-15 05:29:24
【问题描述】:
我正在 Clojure 中进行一项练习(根据是否为问题回答输入,用大写字母等等),虽然它有效,但我无法通过所需的测试,因为 我的代码返回nil 以及每个正确答案。我怎样才能避免这种情况?
(ns bob
(:use [clojure.string :only [upper-case blank?]]))
(defn- silence? [question]
(blank? question))
(defn- yell? [question]
(and (re-find #".*!$" question) (= question (upper-case question))))
(defn- ask? [question]
(re-find #".*\?$" question))
(defn response-for [question]
(if (silence? "Fine, be that way.")
(case ((juxt yell? ask?) question)
[true true] "Calm down, I know what I'm doing!"
[true false] "Woah, chill out!"
[false true] "Sure."
"Whatever.")))
测试示例:
FAIL in (responds-to-forceful-talking) (bob_test.clj:25)
expected: (= "Whatever." (bob/response-for "Let's go make out behind the gym!"))
actual: (not (= "Whatever." nil))
提前谢谢你!
【问题讨论】:
标签: clojure functional-programming null