使用guile的Scheme中的if-else语句错误

问题描述

在这里计划的新手总数。

我一段时间以来一直在解决方案问题。我不知道如何编写此权限。我已经查看了该网站上的每个地方以及其他地方,但是我无法使它正常工作。

问题:定义一个平方函数,对参数进行平方。如果参数不是数字,则显示消息“ 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.

由于'h'不是数字,是否应该打印“ invalid_input”? 请帮我在这里。谢谢

解决方法

函数总是在Scheme中求值。对于model = Sequential() model.add(Masking(mask_value=0,input_shape = (input_shape.shape[1],2))) model.add(Bidirectional(LSTM(128,dropout=0,return_sequences=True,activation='tanh'))) model.add(Bidirectional(LSTM(128,return_sequences=False))) model.add((RepeatVector(targets.shape[1]))) model.add(Bidirectional(LSTM(128,return_sequences=True))) model.add(TimeDistributed(Dense(64,activation='relu'))) model.add(TimeDistributed(Dense(1,activation='linear'))) model.build() model.compile(optimizer=optimizers.Adam(0.00001),loss = 'MAE') (square h)是一个未绑定变量;评估h时,由于未绑定h,因此会发出错误。您可以尝试h(square 'h)来查看OP定义是否按预期工作。在第一种情况下,(square "h")是符号,在第二种情况下,'h是字符串;这些不是数字,因此打印"h"

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

"invalid_input"
,

您的代码可以正常工作。但是您必须定义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));
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...