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.
(let ((weight (progn
(format t "Weight: ")
(read t))))
(if (floatp weight) weight 0))
I guess I should be more clear. I've been learning Lisp for a little over 2 weeks so I'm very new. I do not understand this. I also need to use that "prompt-read" function in the same way I used it in the rest of that function. So if you assumed I'd know how to do that, that's my fault, but I do not.
Looks to work as is. I guess can learn how it works from my professor. Thanks.
I don't know how the function prompt-read works, so I use format instead. In particular, how it stores the entered values. Normally it's bad style -and dangerous- to use global variables, which seems to be what it's doing. That's where the LET comes into play. But the important thing for you is to replace prompt-read with READ, which can read and parse a float.
Note that for improved portability, one needs to call
(finish-output)
, since output streams can be buffered.DO NOT USE THIS in any production code. If you use
READ
without very careful controls around it then someone typing at the prompt can make the Lisp do anything. This is the equivalent of allowing an SQL injection attack, except that the language standard is the documentation on how to do the attack.