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
Clojure 查找第n个元素-函数组合_Clojure - Fatal编程技术网

Clojure 查找第n个元素-函数组合

Clojure 查找第n个元素-函数组合,clojure,Clojure,我目前正在读《Clojure的快乐》,第二版。 在第7章“函数编程”中,我遇到了以下用于描述函数组成的函数 (defn fnth [n] (apply comp (cons first (take (dec n) (repeat rest))))) ((fnth 5) '[a b c d e]) ;=> e 我不完全理解这个函数的机制。 尝试在不使用comp的情况下复制内部作用域,函数将生成一个重复列表: (take (dec 5)

我目前正在读《Clojure的快乐》,第二版。 在第7章“函数编程”中,我遇到了以下用于描述函数组成的函数

(defn fnth [n]
  (apply comp
         (cons first
               (take (dec n) (repeat rest)))))

((fnth 5) '[a b c d e])
;=> e
我不完全理解这个函数的机制。 尝试在不使用
comp
的情况下复制内部作用域,函数将生成一个重复列表:

(take (dec 5) (repeat (rest '[a b c d e])))
;=> ((b c d e) (b c d e) (b c d e) (b c d e))
使用
comp
第一个
附加到该重复列表有什么意义?
这也是送入
comp
的功能列表的最后一部分吗

(first (take (dec 5) (repeat (rest '[a b c d e]))))
=> (b c d e)

提前谢谢你

fnth
函数的工作原理是构造一个形式为
(first rest…rest)
的序列,即
first
后跟n-1
rest
s。然后,它通过
apply
ing
comp
将它们组合成一个函数。对于
(fnth 5)
,这是

(apply comp (first rest rest rest rest))
当对向量调用result函数时,
[a b c d e]
生成的应用程序

(first (rest (rest (rest (rest [a b c d e])))))
i、 e.输入序列第四个尾部的第一个元素


您尝试的重建只调用
rest
一次,然后
reapeat
s返回的序列。

fnth
函数的工作方式是构造一个形式为
(first rest rest…rest)
的序列,即
first
后接n-1
rest
s。然后,它通过
apply
ing
comp
将它们组合成一个函数。对于
(fnth 5)
,这是

(apply comp (first rest rest rest rest))
当对向量调用result函数时,
[a b c d e]
生成的应用程序

(first (rest (rest (rest (rest [a b c d e])))))
i、 e.输入序列第四个尾部的第一个元素


您尝试的重建只调用
rest
一次,然后
reapeat
s返回的序列。

fnth
不会(本身)返回集合的第n项,而是生成一个这样做的函数

它应该产生什么功能来获取第一项

(defn nth-1 [coll]
    (first coll))
    
它应该产生什么功能来获取第二项

(defn nth-2 [coll]
    (first (rest coll)))
    
它应该产生什么功能来获取第三项

(defn nth-3 [coll]
    (first (rest (rest coll))))
    
所以有一个模式!但是嵌套对于
fnth
来说是一个挑战

(first(rest(rest x))
的等效公式是
((comp first rest)x)
。现在我们可以把n-3写成

(defn nth-3 [coll]
    ((comp first rest rest) coll))
    

使用这种方法,
fnth
更容易编写。但是,如果我们将
(comp first rest)
重写为
(apply comp[first rest])
,就更容易了。那里现在,
fnth
需要的是列出first+正确数量的rest,并对该列表应用comp。

fnth
不会(本身)返回集合的第n项,而是生成一个这样做的函数

它应该产生什么功能来获取第一项

(defn nth-1 [coll]
    (first coll))
    
它应该产生什么功能来获取第二项

(defn nth-2 [coll]
    (first (rest coll)))
    
它应该产生什么功能来获取第三项

(defn nth-3 [coll]
    (first (rest (rest coll))))
    
所以有一个模式!但是嵌套对于
fnth
来说是一个挑战

(first(rest(rest x))
的等效公式是
((comp first rest)x)
。现在我们可以把n-3写成

(defn nth-3 [coll]
    ((comp first rest rest) coll))
    

使用这种方法,
fnth
更容易编写。但是,如果我们将
(comp first rest)
重写为
(apply comp[first rest])
,就更容易了。那里现在,
fnth
需要的是列出first+正确数量的rest,并对该列表应用comp。

我明白了。作者也使用了这种方法。这意味着
cons
在将完整列表输入
comp
进行评估之前将
first
附加到函数列表中,对吗?@gitsrush-对
((comp f g)x)
(f(g x))
因此将
放在生成序列的前面意味着它将是应用于输入的最后一个函数。因此,结果合成返回输入序列第n-1个尾部的第一个元素。作者也使用了这种方法。这意味着
cons
在将完整列表输入
comp
进行评估之前将
first
附加到函数列表中,对吗?@gitsrush-对
((comp f g)x)
(f(g x))
因此将
放在生成序列的前面意味着它将是应用于输入的最后一个函数。因此,结果合成返回输入序列第n-1个尾部的第一个元素。