0
votes

I am not understand why instead of normal list I recive (clojure.core/seq). My code

(defn del-list [arg-list lvl] (do
                                (cond
                                 (= lvl 1) (remove seq? arg-list)
                                 :else (map #(if (seq? %)
                                               (del-list % (- lvl 1))
                                               %
                                               ) arg-list)
                                )
                            ))
(println (del-list `(1 2 3 `(1 2 `(1 2 3) 3) 1 2 3) 2))
;result=> (1 2 3 (clojure.core/seq) 1 2 3)

Why is this happening? I don't know how valid search this, all links from google point me to documentation about seq and seq?.

1
I think you want sequential? instead of seq?. - Jeremy
1. Don't use the backtick, just use single quote. 2. Don't nest your quotes. One single one in the beginning is enough - ClojureMostly
I resolve this problem with dont use single quote and backtick in nested lists. (println (del-list '(1 2 3 (1 2 (1 2 3) 3) 1 2 3) 2)) it work. - Nikita Davidenko
But, why don't use backtick? - Nikita Davidenko
All I understand, need use only singlequote. ClojureMostly send you message in answer view. - Nikita Davidenko

1 Answers

1
votes

Like @ClojureMostly says in the comments, don't use bacticks, use a single quote. Also don't nest them, one is enough.

So, calling your function like this:

(println (del-list '(1 2 3 (1 2 (1 2 3) 3) 1 2 3) 2))

Will solve your immediate problem.

Going into a bit more depth, there are some differencences between single quote (just called quote) and backtick (called syntax quote).

In quoting something, you say that you want just the data structure, that it shouldn't be evaluated as code. In clojure, code is data, so (+ 1 2) is a list with a symbol and two numbers which, when evaluated as code, evals to 3. So, (+ 1 2) => 3 and '(+ 1 2) => (+ 1 2).

Syntax quote is similar, but it looks up the namespaces of things and you can unquote stuff inside. This makes it useful for writing macros.

;; 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)

Nested quotes are never what you want. (Unless you alternate quoting and unquoting, and even then it's usually confusing)

In clojure you'd usually represent sequential things as vectors [1 2 3] (O(n) random access, grows at the end), unless you specifically want some property of a linked list for your data. (Like representing a stack, as lists efficiently add and remove the first element.)

(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

As for your actual question, why does clojure.core/seq appear, I have no idea. That's not how you use quoting, so it has never come up.