Clojure查找矩形区域

Clojure查找矩形区域,clojure,Clojure,我对Clojure和 每当我试图编译printf函数时,都会出现无法解决符号错误尽管您提供了一个不完整的示例,但经过一些更改后,您的问题似乎是您正在使用%f格式化整数java.lang.Long: (defn -main [] (println "\nTo compute the area of a rectangle,") (print " enter its width: ") (flush) (let [ Width (read) ] (assert (>=

我对Clojure和
每当我试图编译printf函数时,都会出现无法解决符号错误

尽管您提供了一个不完整的示例,但经过一些更改后,您的问题似乎是您正在使用%f格式化整数java.lang.Long:

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (print   " enter its width: ") (flush)
  (let [ Width (read) ]
    (assert (>= Width 0) "-main: Width must be positive"))
  (print " enter its height: ") (flush)
  (let [ Height (read) ]
    (assert (>= Height 0) "-main: Height must be positive")
    (printf "\nThe area is %f %f\n\n" (rectangleArea Width Height  ))
当然,你的let块是不正确的,所以应该是这样的:

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (print   " enter its width: ") (flush)
  (let [ Width (read) ]
    (assert (>= Width 0) "-main: Width must be positive"))
  (print " enter its height: ") (flush)
  (let [ Height (read) ]
    (assert (>= Height 0) "-main: Height must be positive")
    (printf "\nThe area is %f %f\n\n" (+ 10 20  ))))

(-main)
;;=> 
1. Unhandled java.util.IllegalFormatConversionException
   f != java.lang.Long

虽然您提供了一个不完整的示例,但经过一些更改后,您的问题似乎是您正在使用%f格式化整数java.lang.Long:

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (print   " enter its width: ") (flush)
  (let [ Width (read) ]
    (assert (>= Width 0) "-main: Width must be positive"))
  (print " enter its height: ") (flush)
  (let [ Height (read) ]
    (assert (>= Height 0) "-main: Height must be positive")
    (printf "\nThe area is %f %f\n\n" (rectangleArea Width Height  ))
当然,你的let块是不正确的,所以应该是这样的:

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (print   " enter its width: ") (flush)
  (let [ Width (read) ]
    (assert (>= Width 0) "-main: Width must be positive"))
  (print " enter its height: ") (flush)
  (let [ Height (read) ]
    (assert (>= Height 0) "-main: Height must be positive")
    (printf "\nThe area is %f %f\n\n" (+ 10 20  ))))

(-main)
;;=> 
1. Unhandled java.util.IllegalFormatConversionException
   f != java.lang.Long

你的测试是一个C风格的主程序。不要这样做。以命名函数的形式编写程序,并使用REPL来练习它们。对于初学者来说,这将编译错误与执行错误分开

我已经调用了你的功能区。编译器抽取的第一滴血是

(defn- read-size [label]
  (print  " enter its " label ":")
  (flush)
  (let [num (read)]
    (assert (>= num 0) (str  label " must be positive"))
    num))

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (flush)
  (let [width (read-size "width")
        height (read-size "height")]
    (printf "\nThe area is %d \n\n" (* width height))))

完全正确。您并没有通过let或def赋予矩形区域一个含义。删除它并重试会出现类似错误:

Syntax error compiling at (amuse.clj:24:39).
Unable to resolve symbol: rectangleArea in this context
问题是,如前所述,当您参考宽度时,您超出了let绑定的范围。删除宽度,我们得到一个干净的编译。让我们尝试调用函数:

Syntax error compiling at (amuse.clj:24:39).
Unable to resolve symbol: Width in this context
发生了什么事?对printf的调用计算表达式高度,在本例中,表达式高度尝试将数字8.0作为函数调用

还要尽量避免将输入/输出交互烘焙到函数中。论点和结果更好。您可以使用和返回地图来传达含义。您的函数可能会被写入

=> (area)

To compute the area of a rectangle,
 enter its width: 8
 enter its height: 9
Execution error (ClassCastException) at amuse/area (amuse.clj:24).
java.lang.Long cannot be cast to clojure.lang.IFn
这看起来很奇怪,但很容易使用:

(defn area [& {:keys [height width]}]
  {:area (+ height width)})
这是正确的吗?我不这么认为。我们应该乘,而不是加:

=> (area :height 8 :width 9)
{:area 17}
所以我们的函数本质上是*的同义词

如果我是你,我现在就让REPL承担验证的压力。当你有严肃的工作要做时,你会发现确实很方便


祝您一切顺利。

您的测试是一个C风格的主程序。不要这样做。以命名函数的形式编写程序,并使用REPL来练习它们。对于初学者来说,这将编译错误与执行错误分开

我已经调用了你的功能区。编译器抽取的第一滴血是

(defn- read-size [label]
  (print  " enter its " label ":")
  (flush)
  (let [num (read)]
    (assert (>= num 0) (str  label " must be positive"))
    num))

(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (flush)
  (let [width (read-size "width")
        height (read-size "height")]
    (printf "\nThe area is %d \n\n" (* width height))))

完全正确。您并没有通过let或def赋予矩形区域一个含义。删除它并重试会出现类似错误:

Syntax error compiling at (amuse.clj:24:39).
Unable to resolve symbol: rectangleArea in this context
问题是,如前所述,当您参考宽度时,您超出了let绑定的范围。删除宽度,我们得到一个干净的编译。让我们尝试调用函数:

Syntax error compiling at (amuse.clj:24:39).
Unable to resolve symbol: Width in this context
发生了什么事?对printf的调用计算表达式高度,在本例中,表达式高度尝试将数字8.0作为函数调用

还要尽量避免将输入/输出交互烘焙到函数中。论点和结果更好。您可以使用和返回地图来传达含义。您的函数可能会被写入

=> (area)

To compute the area of a rectangle,
 enter its width: 8
 enter its height: 9
Execution error (ClassCastException) at amuse/area (amuse.clj:24).
java.lang.Long cannot be cast to clojure.lang.IFn
这看起来很奇怪,但很容易使用:

(defn area [& {:keys [height width]}]
  {:area (+ height width)})
这是正确的吗?我不这么认为。我们应该乘,而不是加:

=> (area :height 8 :width 9)
{:area 17}
所以我们的函数本质上是*的同义词

如果我是你,我现在就让REPL承担验证的压力。当你有严肃的工作要做时,你会发现确实很方便


祝您一切顺利。

请编辑您的问题,并添加您遇到的全部代码和错误,以便人们能够更好地帮助您。这似乎是一个问题,可能是宽度仅绑定到第一个断言,但在最后一行,宽度没有定义,因为您过早地关闭了let。另外,最后一行中的格式字符串包含两个%f,但只提供了一个值,因此可能会引发MissingFormatArgumentException。谢谢!我是新手,所以我只是习惯了。下次会做得更好!请编辑您的问题,并添加完整的代码和错误,这样人们可以更好地帮助您。这似乎是一个问题,可能是宽度仅绑定到第一个断言,但在最后一行,宽度没有定义,因为您过早地关闭了let。另外,最后一行中的格式字符串包含两个%f,但只提供了一个值,因此可能会引发MissingFormatArgumentException。谢谢!我是新手,所以我只是习惯了。下次会做得更好!defn rectangleArea[width height]*width height我用它作为主要函数来计算矩形的面积,下面的代码就是执行过程,但似乎不能让它完成work@hkira1那么这是另一个问题吗?这是什么意思?似乎不能让它工作?你能说得更具体些吗?顺便说一句,您在注释中共享的代码中缺少了最后一个右大括号-应该是defn rectangle area[width height]*width heightdefn rectangleArea[width height]*width-height我使用它作为主要函数来计算矩形的面积,下面的代码就是执行过程,但似乎不能让它执行work@hkira1那么这是另一个问题吗?这是什么意思?似乎不能让它工作?你能说得更具体些吗?顺便说一句,你错过了决赛 注释中共享的代码中的右大括号-应为defn矩形区域[width height]*width height