温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Lisp: How to prompt-read a float?
common-lisp lisp clisp slime

其他 - Lisp:如何提示阅读浮动信息?

发布于 2020-04-19 12:51:23

我有一个函数,在其中我同时使用解析整数和提示读取。但是,我需要这些整数之一才能成为浮点数。当我将parse-integer更改为parse-float时,它不再起作用。这是函数:

(defun prompt-for-cat ()                                                       
  (add-record                                                                  
    (make-cat                                                                    
      (prompt-read "Name")                                                        
      (prompt-read "Coloring")                                                    
      (or (parse-integer (prompt-read "Weight") :junk-allowed t) 0)               
      (or (parse-integer (prompt-read "Experience") :junk-allowed t) 0)           
      (or (parse-integer (prompt-read "Length") :junk-allowed t) 0))))  

这按原样工作,但我需要第一个整数“ Weight”为浮点数。parse-float无法正常工作,我找不到正确的方法来执行此操作。

查看更多

提问者
kokko1G
被浏览
30
121k 2020-02-06 04:28
(let ((weight (progn
                (format t "Weight: ")
                (read t))))
  (if (floatp weight) weight 0))