Lisp:如何提示阅读浮动信息? [英] Lisp: How to prompt-read a float?

查看:73
本文介绍了Lisp:如何提示阅读浮动信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

这篇关于Lisp:如何提示阅读浮动信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆