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
Recursion 元素Clojure的返回位置_Recursion_Clojure_Functional Programming_Lisp_Iteration - Fatal编程技术网

Recursion 元素Clojure的返回位置

Recursion 元素Clojure的返回位置,recursion,clojure,functional-programming,lisp,iteration,Recursion,Clojure,Functional Programming,Lisp,Iteration,我正试图找到一个解决我目前面临的问题的方法,我已经尝试了很多次,但都没有成功。我试图扫描一个包含数据的列表,然后返回数据的位置(如果找到) 例如,如果我运行以下命令: (ind 'p '(l m n o p o p)) 然后我会得到一个返回值 ==> 4 6 因为它已经在这些位置找到了数据 我已经接近我认为我想要的这个解决方案之前,但我不能让它运行。有人能帮我弄清楚我的功能出了什么问题吗??就我所知,它应该能起作用,但我不明白为什么不能 (defn ind ([choice li

我正试图找到一个解决我目前面临的问题的方法,我已经尝试了很多次,但都没有成功。我试图扫描一个包含数据的列表,然后返回数据的位置(如果找到)

例如,如果我运行以下命令:

(ind 'p '(l m n o p o p))
然后我会得到一个返回值

==>  4 6  
因为它已经在这些位置找到了数据

我已经接近我认为我想要的这个解决方案之前,但我不能让它运行。有人能帮我弄清楚我的功能出了什么问题吗??就我所知,它应该能起作用,但我不明白为什么不能

(defn ind
([choice list emptylist x]
(let [x (count list)])
(if (= (x) 0)
  nil)
(if (= (first list) item)
  (ind item (rest list) (cons x emptylist) (dec x))
  (ind item (rest list) emptylist (dec x))
  )
 )
)

我试图做的是循环列表,直到它遇到一个值并将其添加到空列表中,一旦循环通过,返回空列表。

以下是一个我认为更简单的解决方案:

(defn find-index 
  "Returns a seq of the indexes of the supplied collection."
  [values target]
  (let [indexes           (range (count values))
        val-idx-tuples    (map vector values indexes) 
        found-tuples      (filter #(= target (first %)) val-idx-tuples)
        found-indexes     (vec (map second found-tuples)) ]
    found-indexes))

(println (find-index '(l m n o p o p) 'p ))

;=> [4 6]

下面是一个我认为更简单的解决方案:

(defn find-index 
  "Returns a seq of the indexes of the supplied collection."
  [values target]
  (let [indexes           (range (count values))
        val-idx-tuples    (map vector values indexes) 
        found-tuples      (filter #(= target (first %)) val-idx-tuples)
        found-indexes     (vec (map second found-tuples)) ]
    found-indexes))

(println (find-index '(l m n o p o p) 'p ))

;=> [4 6]

我发现Clojure中有一个内置函数

因此,您可以简单地执行以下操作:

(keep-indexed (fn [idx elm] (if (= 'p elm) idx)) '(l m n o p o p))
; return (4 6)

我发现Clojure中有一个内置函数

因此,您可以简单地执行以下操作:

(keep-indexed (fn [idx elm] (if (= 'p elm) idx)) '(l m n o p o p))
; return (4 6)
虽然我更喜欢,但您可以按如下方式编写所需的函数:

(defn ind [x coll]
  (loop [ans [], coll coll, n 0]
    (if-let [[y & ys] (seq coll)]
      (recur (if (= x y) (conj ans n) ans) ys (inc n))
      ans)))

(ind 'p '(l m n o p o p))
;[4 6]
这使用了几个成语使其简洁:

if-let包含if-inside-let。 分解表单[y&ys]包括对first和rest的调用。 将if表单下推到recur中可以避免重复。 虽然我更喜欢,但您可以按如下方式编写所需的函数:

(defn ind [x coll]
  (loop [ans [], coll coll, n 0]
    (if-let [[y & ys] (seq coll)]
      (recur (if (= x y) (conj ans n) ans) ys (inc n))
      ans)))

(ind 'p '(l m n o p o p))
;[4 6]
这使用了几个成语使其简洁:

if-let包含if-inside-let。 分解表单[y&ys]包括对first和rest的调用。 将if表单下推到recur中可以避免重复。
建议您将此框定为所需函数:defn ind[x coll]keep indexed fn[idx elm]if=x elm idx collWow,我以前从未见过该函数。建议您将此框定为所需函数:defn ind[x coll]keep indexed fn[idx elm]if=x elm idx collWow,我以前从未见过该函数。您可以不使用索引。只需说让[val idx元组映射向量值范围…]。。。。当任何集合用完时,映射终止。使用“贴图索引”而不是“贴图”,可以获得类似的效果。我还省略了最后一个vec。然后整个事情都是懒惰的,所以客户可以决定如何以及何时实现它。你可以不用索引。只需说让[val idx元组映射向量值范围…]。。。。当任何集合用完时,映射终止。使用“贴图索引”而不是“贴图”,可以获得类似的效果。我还省略了最后一个vec。然后整个事情都是懒惰的,所以客户可以决定如何以及何时实现它。我相信这是一个重复我相信这是一个重复