Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(clojure.core/seq)而不是列表_Clojure - Fatal编程技术网

(clojure.core/seq)而不是列表

(clojure.core/seq)而不是列表,clojure,Clojure,我不明白为什么我收到的不是普通的列表(clojure.core/seq)。 我的代码 为什么会这样?我不知道搜索这个有多有效,所有来自谷歌的链接都指向我关于seq和seq?的文档,就像@ClojureMostly在评论中说的,不要使用标签,使用一个引号。也不要把它们窝起来,一个就够了 这样调用函数: (println (del-list '(1 2 3 (1 2 (1 2 3) 3) 1 2 3) 2)) 会解决你眼前的问题 更深入一点,单引号(仅称为引号)和反勾号(称为语法引号)之间存在一些

我不明白为什么我收到的不是普通的列表(clojure.core/seq)。 我的代码


为什么会这样?我不知道搜索这个有多有效,所有来自谷歌的链接都指向我关于
seq
seq?

的文档,就像@ClojureMostly在评论中说的,不要使用标签,使用一个引号。也不要把它们窝起来,一个就够了

这样调用函数:

(println (del-list '(1 2 3 (1 2 (1 2 3) 3) 1 2 3) 2))
会解决你眼前的问题

更深入一点,单引号(仅称为引号)和反勾号(称为语法引号)之间存在一些差异

在引用某些内容时,您说您只需要数据结构,而不应将其作为代码进行计算。在clojure中,代码是数据,因此
(+12)
是一个带有符号和两个数字的列表,当作为代码进行计算时,会计算为3。所以,
(+12)
=>
3
”(+12)
=>
(+12)

语法quote类似,但它查找事物的名称空间,您可以在其中取消引用。这对于编写宏非常有用

;; Looks up the namespaces of symbols to avoid the problem of variable capture
`(+ 1 2) ;=> (clojure.core/+ 1 2)

;; You can unquote parts of the expression.
;; ~ is unquote ~@ is unqoute splicing
;; That should give you the vocabulary to google this.
`(+ 1 2 3 ~(* 2 2)) ;=> (clojure.core/+ 1 2 3 4)
嵌套引号永远不是您想要的。(除非你交替引用和取消引用,即使这样,通常也会令人困惑)

在clojure中,您通常将连续的事物表示为向量
[1 2 3]
(O(n)随机访问,在末尾增长),除非您特别希望数据具有链表的某些属性。(就像表示堆栈一样,as列表可以有效地添加和删除第一个元素。)


至于你的实际问题,为什么clojure.core/seq会出现,我不知道。这不是你使用报价的方式,所以它从来没有出现过。

我想你想要的是
sequential?
而不是
seq?
.1。不要使用反勾号,只使用单引号。2.不要套用你的话。开头的一个问题是Enough我解决了这个问题,在嵌套列表中不使用单引号和反勾号<代码>(println(del list)(1 2 3(1 2(1 2 3)3)1 2 3)2))它可以工作。但是,为什么不使用backtick?据我所知,只需要使用singlequote。Clojure通常会在应答视图中向您发送消息。但如果使用
则可能会使通用功能发挥作用?@NickitaDavidenko嗯,这有一些复杂性。如果您使用
(into(empty xs)(remove p?xs))
则会得到相同的类型,但由于列表在前面添加,向量在后面添加,因此列表会颠倒。如果您真的需要创建一个独立于类型的函数,该函数返回的类型与传入的类型相同,我会使用库。这完全取决于你要用这个函数做什么。
;; Looks up the namespaces of symbols to avoid the problem of variable capture
`(+ 1 2) ;=> (clojure.core/+ 1 2)

;; You can unquote parts of the expression.
;; ~ is unquote ~@ is unqoute splicing
;; That should give you the vocabulary to google this.
`(+ 1 2 3 ~(* 2 2)) ;=> (clojure.core/+ 1 2 3 4)
(defn del-list [arg-list lvl]
  ;; You don't need the do, there's an implicit do in many special forms
  ;; like let, fn and defn
  ;; Also you only had one thing in your do, in that case it doesn't do anything

  ;; There's only one condition, so I'd use if instead of cond
  (if (= lvl 1)

    ;; And, like someone mentioned in the comments,
    ;; what you probably want is sequential? instead of seq?
    (remove sequential? arg-list)
    (map #(if (sequential? %)
            (del-list % (dec lvl)) ; dec stands for decrement
            %)
         arg-list)))


;; This works
(println (del-list '(1 2 3 (1 2 (1 2 3) 3) 1 2 3) 2))

;; But this is more idiomatic (modulo specific reasons to prefer lists)
(println (del-list [1 2 3 [1 2 [1 2 3] 3] 1 2 3] 2))

;; If you change map to mapv, and wrap remove with vec it will return vectors
(defn del-vec [arg-vec lvl]
  (if (= lvl 1)
    (vec (remove sequential? arg-vec))
    (mapv #(if (sequential? %)
             (del-vec % (dec lvl)) ; dec stands for decrement
             %)
          arg-vec)))

(println (del-vec [1 2 3 [1 2 [1 2 3] 3] 1 2 3] 2))

;; But most of the time you don't care about the specific type of sequential things