【问题标题】:Multiple function calls within a function allowed in Clojure?Clojure允许在一个函数中调用多个函数?
【发布时间】:2019-11-14 03:11:15
【问题描述】:

我正在尝试从城市哈希图中获取某些值来实现我的距离函数:

Distance Function

但每次我都说不支持的绑定形式:(latReturn record1)

这是我的 latReturn 和 longReturn 函数:

(defn latReturn [line]
    (def x line)
    (def y (string/split x #","))
    (def my-map { :city (y 0)
               :lat (y 1)
               :long (y 2)
               :country (y 3)
               :iso2 (y 4)
               :admin (y 5)
               :capital (y 6)
               :pop (y 7)
               :popproper (y 8)
    })
    (read-string (my-map :lat))
)

(defn longReturn [line]
    (def x line)
    (def y (string/split x #","))
    (def my-map { :city (y 0)
               :lat (y 1)
               :long (y 2)
               :country (y 3)
               :iso2 (y 4)
               :admin (y 5)
               :capital (y 6)
               :pop (y 7)
               :popproper (y 8)
    })
    (read-string (my-map :long))
)

我在这里做错了什么?

【问题讨论】:

  • 输入line 是什么样的?还有为什么 def 在函数内?通常应该使用let绑定
  • Toronto,43.666667,-79.416667,Canada,CA,Ontario,admin,5213000,3934421 这是可以发送到 line 的行之一。另外,是的,我一直想将 def 更改为 let 绑定
  • def inside defn 在 clojure 中是一个禁忌。请改用let。

标签: function clojure hashmap call


【解决方案1】:

在您对let 的调用中,您的距离函数绑定错误。请阅读一些 Clojure 介绍材料,了解应该如何使用 let。

基本上是这样的:

(let [binding1 value1
      binding2 value2]
   (some-things-to-do-with binding1 binding2))

但是你在括号中有一个东西作为绑定:

(let [...
      (latReturn record1) (Math/toRadians ...)
      ...]
   ...)

您不能分配给(latReturn record1),因为这是一个函数调用。

正如其他人已经指出的那样,不要在def 或defn 中使用def。它总是错误的(或者更确切地说,它总是错误的,直到你学会了足够的 Clojure 来知道它可能是正确的非常非常罕见的情况,即使这样它通常也不是)。

【讨论】:

  • 感谢我将函数调用更改为变量名并修复了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多