如何避免返回';无';在Clojure的字符串之后?
我正在用Clojure做一个练习(根据是否是一个问题回答输入,用大写字母等写),当它工作时,我无法通过要求的测试,因为我的代码返回如何避免返回';无';在Clojure的字符串之后?,clojure,functional-programming,null,Clojure,Functional Programming,Null,我正在用Clojure做一个练习(根据是否是一个问题回答输入,用大写字母等写),当它工作时,我无法通过要求的测试,因为我的代码返回nil以及所有正确的答案。我怎样才能避免呢 (ns bob (:use [clojure.string :only [upper-case blank?]])) (defn- silence? [question] (blank? question)) (defn- yell? [question] (and (re-find #".*!$&
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))
提前谢谢你 您对方法的
响应实际上一直在返回nil
。您可能打算将if
条件设置为(静默?问题)
,但实际上它被应用于您打算设置为“then”子句的字符串文字。由于这是当前意外构造的,“else”是nil
您对方法的响应实际上一直在返回nil
。您可能打算将if
条件设置为(静默?问题)
,但实际上它被应用于您打算设置为“then”子句的字符串文字。由于这是当前意外构造的,“else”为nil
,正如John在中评论的那样,如果,则会进行一些调整以修复它:
(defn response-for [question]
(if (silence? question)
"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.")))
(= "Whatever." (bob/response-for "Let's go make out behind the gym!"))
正如John评论的那样,您的问题在if中,请进行以下调整以解决它:
(defn response-for [question]
(if (silence? question)
"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.")))
(= "Whatever." (bob/response-for "Let's go make out behind the gym!"))