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中的函数定义_Clojure - Fatal编程技术网

Clojure中的函数定义

Clojure中的函数定义,clojure,Clojure,我试图使用parse ez,但我不明白为什么我会得到这样的结果。当我在函数中定义我认为是等价的代码时,我会得到不同的结果: (use 'protoflex.parse) ; ; Use an anonymous function, this returns [1 1] (parse #(line-pos) "") ; ; Use an actual function, this returns what I think is a function pointer (defn fooParse [

我试图使用parse ez,但我不明白为什么我会得到这样的结果。当我在函数中定义我认为是等价的代码时,我会得到不同的结果:

(use 'protoflex.parse)
;
; Use an anonymous function, this returns [1 1]
(parse #(line-pos) "")
;
; Use an actual function, this returns what I think is a function pointer
(defn fooParse [] line-pos)
(parse fooParse "")

有什么区别吗?

您必须在
fooprase
内调用
line pos
。像这样:

(defn fooParse [] (line-pos))
正如你在照片中看到的。读取器宏
#()
扩展为:

#(...) => (fn [args] (...))

您必须在
fooParse
内调用
line pos
。像这样:

(defn fooParse [] (line-pos))
正如你在照片中看到的。读取器宏
#()
扩展为:

#(...) => (fn [args] (...))

要在Clojure中调用函数,需要

(my-function)
另一方面,如果你说

my-function
这只是对函数的引用。(“Reference”在这里不是一个技术术语,但我认为这清楚地说明了我的意思。)在第二个示例中,函数
fooParse
的“返回值”是函数的第二种形式,它是
line pos
,而不是
(line pos)
-因此,
fooParse
返回的对象是对函数
line pos
的引用,而不是
line pos
的返回值。我想你想要的是

(defn fooParse
  []
  (line-pos))

要在Clojure中调用函数,需要

(my-function)
另一方面,如果你说

my-function
这只是对函数的引用。(“Reference”在这里不是一个技术术语,但我认为这清楚地说明了我的意思。)在第二个示例中,函数
fooParse
的“返回值”是函数的第二种形式,它是
line pos
,而不是
(line pos)
-因此,
fooParse
返回的对象是对函数
line pos
的引用,而不是
line pos
的返回值。我想你想要的是

(defn fooParse
  []
  (line-pos))