不能取宏的价值(clojure)

11 compiler-errors clojure

在这段clojure代码中:

(defn makeStructs ;line 27
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
   with-open[r (reader (file fName))]
   (let [r 
      res (doall (map makeStruct (line-seq r)))
      ]    
  (. r close)
     res
  ) 
)
Run Code Online (Sandbox Code Playgroud)

我收到此编译器错误:

Exception in thread "main" java.lang.Exception: Can't take value of a macro: #'clojure.core/with-open (clojureHW.clj:27)
Run Code Online (Sandbox Code Playgroud)

第27行在上面评论.

知道问题是什么吗?

Ale*_*x B 8

你需要实际调用宏,

(defn makeStructs ;line 27
 "..."
 [fName]
   (with-open ; note the extra paren
Run Code Online (Sandbox Code Playgroud)

  • 它口齿不清地给出了答案,但它周围有括号:) (3认同)

nic*_*kik 7

(defn makeStructs ;line 27
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
   with-open[r (reader (file fName))]
   (let [r 
      res (doall (map makeStruct (line-seq r)))
      ]    
  (. r close)
     res
  ) 
)
Run Code Online (Sandbox Code Playgroud)

这不能工作,因为

  1. 你周围没有parens with-open.这将是一个正常的符号,它不被称为.
  2. 您的let中的表单数量不一致.你有r,res aund(doall ......).你已经在with-open中为'r'做了正确的绑定.所有你需要的是

    (让[res(doall(map makeStruct(line-seq r)))] ....)

  3. 你为什么(. r close)要用open-open宏来看看这个:http://clojuredocs.org/clojure_core/clojure.core/with-open

所以你得到:

(defn makeStructs 
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
   (with-open [r (reader (file fName))]
     (let [res (doall (map makeStruct (line-seq r)))]
        res)))
Run Code Online (Sandbox Code Playgroud)

但是因为你只有一件事让你不需要:

(defn makeStructs 
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
   (with-open [r (reader (file fName))]
     (doall (map makeStruct (line-seq r)))))
Run Code Online (Sandbox Code Playgroud)

Lisp语言非常简单,大多数程序员只是想让自己变得困难,因为他们已经习惯了.你遇到的很多问题都是因为你已经习惯了像X这样的工作,但现在他们像Y一样工作.尽量不要认为事情就像X一样,你会让你的生活更轻松.