【问题标题】:If-else statement error in Scheme using guile使用 guile 的 Scheme 中的 if-else 语句错误
【发布时间】: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


【解决方案1】:

函数总是在 Scheme 中评估它们的参数。对于(square h)h 是一个未绑定的变量;在评估 h 时,由于未绑定 h,因此会发出错误。通过尝试(square 'h)(square "h"),您可以看到 OP 定义按预期工作。在第一种情况下'h 是一个符号,在第二种情况下"h" 是一个字符串;这些不是数字,所以会打印"invalid_input"

在格式化 Scheme 代码时,OP 应该遵循 lispy 约定;在 lisps 中将右括号分散在多行上是非常糟糕的风格。下面是更易读的相同代码,典型的 lisps:

(define (square x)
  (cond ((number? x) (* x x))
        (else
         (display "invalid_input\n"))))

【讨论】:

  • 噢噢噢噢。我明白你的意思了。谢谢你的提示。昨天确实是我第一次尝试 Scheme 或任何 Lisp 后代。
【解决方案2】:

您的代码运行良好。但是您必须定义 h 才能使用它。

$ guile
GNU Guile 2.0.13
Copyright (C) 1995-2016 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.

你的函数是正确的。

scheme@(guile-user)> (define (square x)
  (if (number? x)
      (* x x)
      (display "invalid_input\n")))

并且按预期工作:

scheme@(guile-user)> (square 5)
$1 = 25

但是如果你传递一个未定义的值,你会得到一个错误:

scheme@(guile-user)> (square h)
;;; <stdin>:6:0: warning: possibly unbound variable `h'
<unnamed port>:6:0: In procedure #<procedure 5595e9575c20 at <current input>:6:0 ()>:
<unnamed port>:6:0: In procedure module-lookup: Unbound variable: h

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,q

您必须确保已定义 h

scheme@(guile-user)> (let ((h 5)) (square h))
$2 = 25

这与所有其他语言相同。在 C 中尝试:

int square (int x) {
  return x * x;
}

int main (int argc, char** argv)
{
  printf ("%d", square (h));
}

【讨论】:

  • 这与其他所有语言都一样”不正确。一些语言为未初始化的变量提供默认值;例如,在 Lua 中,未绑定的 h 将默认为 nil。即使在 C 中,静态变量也是零初始化的。
  • @exnihilo 如果您声明它们,它们将具有默认值。但他甚至没有声明变量。
  • 在 Lua 或其他一些语言中不需要声明变量。
  • @exnihilo 在我看来,如果 Lua 只是另一个 Bash,我是对的。
  • @ceving:嗯,在 shell 脚本中你可以说set -u 来解决这个问题。我不知道 Lua 是否有等价物:我希望如此。
猜你喜欢
  • 2012-06-30
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 2020-09-20
  • 2018-12-22
  • 1970-01-01
相关资源
最近更新 更多