【发布时间】:2020-12-19 06:30:57
【问题描述】:
这里是 Scheme 的新手。
一段时间以来,我一直被一个方案问题所困扰。我不明白如何正确编码。我已经查看了这个网站和其他网站上的所有地方,但我无法让它工作。
问题:定义一个平方其参数的函数 Square。如果参数不是数字,则打印消息“invalid_input”。
这是我尝试过的:
(define (square x) (cond
((number? x) (* x x))
(else (display "invalid_input\n"))
)
)
我也试过这个:
(define (square x) (cond
((number? x) (* x x))
((not (number? x)) (display "invalid_input\n"))
)
)
还有这个:
(define (square x) (if
(number? x) (* x x) (display "invalid_input\n")
)
)
当我像这样调用正方形(正方形 h)时,这些都不起作用。 我在 Linux 上不断收到此错误
scheme@(guile-user)> (square h)
;;; <stdin>:44:0: warning: possibly unbound variable `h'
<unnamed port>:44:0: In procedure #<procedure 7fcc7d0a0b60 at <current input>:44:0 ()>:
<unnamed port>:44:0: In procedure module-lookup: Unbound variable: h
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
不应该打印“invalid_input”,因为“h”不是数字吗? 请帮帮我。谢谢
【问题讨论】:
-
个人轶事:在我放弃复制我在其他语言中使用的布局之前,我一直在努力理解 lisp-y 语言的结构。 Lisp 不是一种块结构的语言,让它看起来像一个只会让人困惑。
标签: if-statement scheme guile negation