Warm tip: This article is reproduced from stackoverflow.com, please click
common-lisp lisp clisp slime

Lisp: How to prompt-read a float?

发布于 2020-04-19 09:29:48

I have a function where I am using parse-integer and prompt-read together. However, I need one of these integers to be a float. When I change parse-integer to parse-float it no longer works. Here is the function:

(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))))  

This works as is, but I need that first integer, "Weight" to be a float. parse-float does not work and I cannot find the correct way to do this.

Questioner
kokko1G
Viewed
46
121k 2020-02-06 04:28
(let ((weight (progn
                (format t "Weight: ")
                (read t))))
  (if (floatp weight) weight 0))