gpt4 book ai didi

testing - 返回值不是 (quote )

转载 作者:行者123 更新时间:2023-11-28 20:02:47 25 4
gpt4 key购买 nike

我正在尝试 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)

它生成以下输出。

λ 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.

它似乎给出了正确的返回值,但格式错误。有什么想法吗?

最佳答案

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

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

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

您编写的代码存在三处错误。

  1. 条件 (and (seq? x) (vector? x)) 永远不会为真。将鼠标悬停在剧透上。

    您应该将 and 更改为 or 或使用 sequential? 代替。

  2. 修复了上述问题后,由于 map 返回序列,您的结构现在将更改为序列结构。这些序列需要递归地连接起来。将鼠标悬停在剧透上。

    map 更改为 mapcat

  3. 现在代码暂时坏了。您需要保护基本案例免受串联操作的影响。将鼠标悬停在剧透上。

    将基本情况返回值 x 包装在一个集合中,例如[x]

关于testing - 返回值不是 (quote <value>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com