I'm trying to use Ubuntu's cl-parse-number package.
I've written the following code in CLISP:
(asdf:operate 'asdf:load-op 'parse-number)
(defun parseable-as-number-p (string)
"Can a string be converted to a number?"
(handler-case
(progn
(parse-number:parse-number string)
t)
(invalid-number () nil)))
(parseable-as-number-p "12.3p")
When I run the code, I get:
*** - PARSE-INTEGER: substring "3p" does not have integer syntax at
position 1
The following restarts are available:
My aim is: the function parseable-as-number-p tries to see if `string'
can be parsed as a number. If it can, it returns t, otherwise it returns
nil.
But it's not working. I've looked in parse-number.lisp, and it mentions
a condition called invalid-number. It doesn't export this as a symbol -
I'm not sure if that's important or not.
So how do I know what the condition is, and hence do something when it
arises?
thus spoke mark carter <··@privacy.net>:
> I'm trying to use Ubuntu's cl-parse-number package.
> I've written the following code in CLISP:
> (asdf:operate 'asdf:load-op 'parse-number)
> (defun parseable-as-number-p (string)
> "Can a string be converted to a number?"
> (handler-case
> (progn
> (parse-number:parse-number string)
> t)
> (invalid-number () nil)))
> But it's not working. I've looked in parse-number.lisp, and it
> mentions a condition called invalid-number. It doesn't export this as
> a symbol - I'm not sure if that's important or not.
Refer to it as `parse-number::invalid-number'. Moreover, the lambda-list
is wrong; the error-clause has to look like this:
| (parse-number::invalid-number (c)
| (declare (ignore c))
| nil)
--
mirrors are more fun than television
Stanisław Halik wrote:
> Refer to it as `parse-number::invalid-number'. Moreover, the lambda-list
> is wrong; the error-clause has to look like this:
>
> | (parse-number::invalid-number (c)
> | (declare (ignore c))
> | nil)
>
Many thanks. That helped. Puzzled by further errors, I finally realised
that the code I saw on the web was not the same as that included in
Ubuntu (which was in compiled form). So, in fact, I also worked out that
parse-number::invalid-number should be parse-number::parse-error.
Actually, I discovered that I made some conceptual errors on some other
parts of the code I wrote, so I've got some ways to go before I reach
takeoff velocity on Lisp.
mark carter wrote:
> Stanisław Halik wrote:
>
>> Refer to it as `parse-number::invalid-number'. Moreover, the lambda-list
>> is wrong; the error-clause has to look like this:
>>
>> | (parse-number::invalid-number (c)
>> | (declare (ignore c))
>> | nil)
>>
>
> Many thanks. That helped. Puzzled by further errors, I finally realised
> that the code I saw on the web was not the same as that included in
> Ubuntu (which was in compiled form). So, in fact, I also worked out that
> parse-number::invalid-number should be parse-number::parse-error.
And if you know that PARSE-ERROR is the only error type that
PARSE-NUMBER will ever signal, you can also do:
(defun parseable-as-number-p (string)
(ignore-errors (parse-number:parse-number string)))
Alberto