无法解析符号:在此上下文中的示例clojure 1.10

发布于 2025-01-24 18:23:48 字数 700 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2025-01-31 18:23:49

我想您没有评估该功能。打开此项目的重复,评估示例的定义,然后评估(示例)

正如您已经在评论中看到的那样,此代码非常糟糕,您不应该从该教程中学习。但是从初学者的角度来看,看到到底是什么是错误的:

  • 命名约定:函数的名称应为dash-epared-partercase-words,所以否示例,但示例
  • 已经提到的 def 内部defndef创建一个全局变量,不要在任何其他定义中使用它。如果需要在函数中创建一些变量,请使用 let
  • 整数变量 float变量。 clojure使用double,并且创建的变量将完全具有这些类型 - 您可以自己使用函数 type
  • 重复 println println (它也可以使用,但您也可以使用 clojure.string/join 带有 vector ,只有一个println获得相同的结果

) :

(defn example []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println (clojure.string/join
               "\n"
               [x (type x) y (type y) str1]))))

(example)

=>

1
class java.lang.Long
1.25
class java.lang.Double
Hello

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:

  • naming conventions: names of functions should be dash-separated-lowercase-words, so no Example, but example.
  • already mentioned def inside defn. Def creates a global variable, don't use it inside any other definitions. If you need to create some variables inside function, use let.
  • integer variable and float variable. Clojure uses long and double and created variables will have exactly these types- you can check it yourself with function type.
  • repeated println (it'll work, but you can also use clojure.string/join with vector and only one println to get the same result)

Improved code:

(defn example []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println (clojure.string/join
               "\n"
               [x (type x) y (type y) str1]))))

(example)

=>

1
class java.lang.Long
1.25
class java.lang.Double
Hello
双手揣兜 2025-01-31 18:23:49

您无法在功能中声明定义。

您需要在外面声明这些符号(在其他langs中的变量)。这样的东西。

(ns com.play

ground.core
  (:gen-class))

(def x 1)
(def y 1.25)
(def str1 "Hello")

(defn Example []
  (println x)
  (println y)
  (println str1))

(Example)

因此,当您这样做时,您正在声明x ystr1作为全局,这不是一个好练习。

为了将这些符号的上下文保留在功能中,您需要使用LET方法

(ns com.playground.core
  (:gen-class))

(defn Example []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println x)
    (println y)
    (println str1)))

(Example)


考虑使用烤肉串案例:)

(ns com.playground.core
  (:gen-class))

(defn example-function []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println x)
    (println y)
    (println str1)))

(example-function)

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

ground.core
  (:gen-class))

(def x 1)
(def y 1.25)
(def str1 "Hello")

(defn Example []
  (println x)
  (println y)
  (println str1))

(Example)

So, when you do it, you're declaring x y and str1 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.

(ns com.playground.core
  (:gen-class))

(defn Example []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println x)
    (println y)
    (println str1)))

(Example)

An extra tip is to avoid using camel-case as a way of declaration naming.
Consider to use kebab-case :)

(ns com.playground.core
  (:gen-class))

(defn example-function []
  (let [x 1
        y 1.25
        str1 "Hello"]
    (println x)
    (println y)
    (println str1)))

(example-function)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文