无法解析符号:在此上下文中的示例clojure 1.10
我是Clojure的初学者,在尝试用Clojure编写代码的同时,我收到了这个错误:
; Syntax error compiling at (src/com/playground/core.clj:17:1).
; Unable to resolve symbol: Example in this context
这是我的代码
(ns com.playground.core
(:gen-class))
;; This program displays Hello World
(defn Example []
;; The below code declares a integer variable
(def x 1)
;; The below code declares a float variable
(def y 1.25)
;; The below code declares a string variable
(def str1 "Hello")
(println x)
(println y)
(println str1))
(Example)
直接从TutorialSpoint撤出,并试图找到其他有相同错误但找不到其他人的人。
I am a beginner with Clojure and I received this error while trying to write code in Clojure:
; Syntax error compiling at (src/com/playground/core.clj:17:1).
; Unable to resolve symbol: Example in this context
Here is my code
(ns com.playground.core
(:gen-class))
;; This program displays Hello World
(defn Example []
;; The below code declares a integer variable
(def x 1)
;; The below code declares a float variable
(def y 1.25)
;; The below code declares a string variable
(def str1 "Hello")
(println x)
(println y)
(println str1))
(Example)
I pulled this directly from tutorialspoint and tried to find other people who had the same error but could not find anybody else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您没有评估该功能。打开此项目的重复,评估
示例
的定义,然后评估(示例)
。正如您已经在评论中看到的那样,此代码非常糟糕,您不应该从该教程中学习。但是从初学者的角度来看,看到到底是什么是错误的:
示例,但
示例
。def 内部
defn
。def
创建一个全局变量,不要在任何其他定义中使用它。如果需要在函数中创建一些变量,请使用let 。
长
和double
,并且创建的变量将完全具有这些类型 - 您可以自己使用函数type
。println
println (它也可以使用,但您也可以使用clojure.string/join
带有vector
,只有一个println
获得相同的结果) :
I guess you didn't evaluate that function. Open REPL for this project, evaluate definition for
Example
and then evaluate(Example)
.As you can already see in comments, this code is very bad and you shouldn't learn from that tutorial. But from a beginner point of view, it may be helpful to see what exactly is wrong:
Example
, butexample
.def
insidedefn
.Def
creates a global variable, don't use it inside any other definitions. If you need to create some variables inside function, uselet
.long
anddouble
and created variables will have exactly these types- you can check it yourself with functiontype
.println
(it'll work, but you can also useclojure.string/join
withvector
and only oneprintln
to get the same result)Improved code:
您无法在功能中声明定义。
您需要在外面声明这些符号(在其他langs中的变量)。这样的东西。
(ns com.play
因此,当您这样做时,您正在声明
x
y
和str1
作为全局,这不是一个好练习。为了将这些符号的上下文保留在功能中,您需要使用
LET
方法。
考虑使用烤肉串案例:)
You can't declare a definition inside a function.
You need to declare these symbols (variable in other langs) outside. Something like that.
(ns com.play
So, when you do it, you're declaring
x
y
andstr1
as global and it's not a good practice.To keep the context of these symbols only inside the function, you need to use the
let
approach.An extra tip is to avoid using camel-case as a way of declaration naming.
Consider to use kebab-case :)