返回值不是(quote <value>)

Kri*_*son 2 testing clojure quote

我正在尝试4clojure上的挑战,并陷入以下代码

(ns com.koddsson.for-clojure
  (:use [clojure.test :only [is deftest run-tests]]))

(defn my-flatten
  ([x] (if (not (and (seq? x) (vector? x)))
           x                      ; If x is not a sequence nor a vector
           (map my-flatten x))))  ; else recursivly apply the flatten function

(deftest test28
  (is (= (my-flatten '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6)))
  (is (= (my-flatten ["a" ["b"] "c"]) '("a" "b" "c")))
  (is (= (my-flatten '((((:a))))) '(:a))))

(run-tests)
Run Code Online (Sandbox Code Playgroud)

它生成以下输出.

? bubblegum 20 ? ? git master* ? lein exec -p 28.clj

Testing com.koddsson.for-clojure

FAIL in (test28) (28.clj:10)
expected: (= (my-flatten (quote ((1 2) 3 [4 [5 6]]))) (quote (1 2 3 4 5 6)))
  actual: (not (= ((1 2) 3 [4 [5 6]]) (1 2 3 4 5 6)))

FAIL in (test28) (28.clj:11)
expected: (= (my-flatten ["a" ["b"] "c"]) (quote ("a" "b" "c")))
  actual: (not (= ["a" ["b"] "c"] ("a" "b" "c")))

FAIL in (test28) (28.clj:12)
expected: (= (my-flatten (quote ((((:a)))))) (quote (:a)))
  actual: (not (= ((((:a)))) (:a)))

Ran 1 tests containing 3 assertions.
3 failures, 0 errors.
Run Code Online (Sandbox Code Playgroud)

似乎它提供了正确的返回值,但格式错误.有任何想法吗?

A. *_*ebb 7

您的测试输出实际上表明您没有获得正确的返回值.

FAIL in (test28) (28.clj:10)
expected: (= (my-flatten (quote ((1 2) 3 [4 [5 6]]))) (quote (1 2 3 4 5 6)))
  actual: (not (= ((1 2) 3 [4 [5 6]]) (1 2 3 4 5 6)))
                  ^^^^^^^^^^^^^^^^^^^
                  output of my-flatten
Run Code Online (Sandbox Code Playgroud)

您应该在REPL中验证您my-flatten返回上面标记的输出.实际上,您的功能本质上是身份功能.

您编写的代码有三个问题.

  1. 条件(and (seq? x) (vector? x))永远不会成立.将鼠标悬停在扰流板上.

    您应该更改andor或使用sequential?.

  2. 通过上述修复,您的结构现在将由于map返回序列而变为序列结构.这些序列需要递归连接.将鼠标悬停在扰流板上.

    更改mapmapcat

  3. 现在代码暂时中断了.您需要保护基本案例不受串联操作的影响.将鼠标悬停在扰流板上.

    将基本案例返回值包装x在集合中,例如[x].