我想要一个包含integer来自用户输入的 的变量。它不能接受字符串和十进制数字。
我需要一些帮助来理解我在这里做错了什么。
我的代码直到现在:
我很感激你的帮助。
(format t "~%Enter a number: ")
(loop (defvar numb (read))
(cond (((rationalp numb)1)
(print "No decimal numbers are allowed, please enter an integer"))
(((stringp numb)1)
(print "No strings are allowed, please enter an integer"))
)
(when ((integerp numb)1) (return numb))
)
Run Code Online (Sandbox Code Playgroud)
这是我将如何做到的:
(defun ask-and-read (prompt)
"Prompt the user and read his input."
(princ prompt *query-io*)
(force-output *query-io*) ; flush the buffers
(let ((*read-eval* nil)) ; close the security hole
(read *query-io*)))
(defun request-object (prompt predicate)
"Ask the user for an object using prompt.
Only accept data which satisfies the predicate."
(loop
for object = (ask-and-read prompt)
when (funcall predicate object)
return object
do (format *query-io* "Alas, ~S (~S) does not satisfy ~S, please try again~%"
object (type-of object) predicate)))
Run Code Online (Sandbox Code Playgroud)
例子:
> (request-object "Enter an integer: " #'integerp)
Enter an integer: 4.6
Alas, 4.6 (SINGLE-FLOAT) does not satisfy #<SYSTEM-FUNCTION INTEGERP>, please try again
Enter an integer: 5/7
Alas, 5/7 (RATIO) does not satisfy #<SYSTEM-FUNCTION INTEGERP>, please try again
Enter an integer: asdf
Alas, ASDF (SYMBOL) does not satisfy #<SYSTEM-FUNCTION INTEGERP>, please try again
Enter an integer: 7
==> 7
> (request-object "Enter a real: " #'realp)
Enter a real: 4.5
==> 4.5
> (request-object "Enter a real: " #'realp)
Enter a real: 5/8
==> 5/8
> (request-object "Enter a real: " #'realp)
Enter a real: "sdf"
Alas, "sdf" ((SIMPLE-BASE-STRING 3)) does not satisfy #<SYSTEM-FUNCTION REALP>, please try again
Enter a real: 8
==> 8
Run Code Online (Sandbox Code Playgroud)
请参阅我使用的设施的文档:
您的代码不可读,因为您的缩进不正确。Lispers 不计算括号 - 这是编译器和编辑器的工作。我们看缩进。请帮自己一个忙并使用 Emacs - 它会为您缩进代码,并且您自己经常会看到您的错误。
首先,defvar是一个顶级表单,用于定义全局变量,而不是设置它们。后续调用不会更改该值:
(defvar *abc* 1)
*abc*
==> 1
(defvar *abc* 10)
*abc*
==> 1 ; not 10!
Run Code Online (Sandbox Code Playgroud)
使用setq到集变量。
虽然 Lisp 确实允许使用全局变量,但 Lisp 中主要的编程风格是函数式风格:每个函数接收它的“输入”数据作为参数,并返回它的“输出”数据作为值。为了实现函数式风格,更喜欢局部变量而不是全局变量。您可以通过let或
let*或 在 中创建局部变量loop,请参阅
局部变量初始化。
请记住,括号在 Lisp 中是有意义的。
如果用户
响应您的提示而进入,则必须绑定*read-eval*到nil
beforeread以避免核战争#.(launch-nuclear-missiles),因为通常read评估之后的任何内容#.。