"황호연" <hyh...@cs.hongik.ac.kr> writes:
> I want a function which transform a string into number (Yea same as
> atoi() in C)
"parse-integer" is what you are looking for.
> (1+ "666")
*** - argument to 1+ should be a number: "666"
1. Break >
> (1+ (parse-integer "666"))
The HyperSpec can be downloaded from this page:
(see the bottom of the page)
> I'm novice in LISP.
> I want a function which transform a string into number (Yea same as atoi()
> in C)
> is there any standard library function which is identical to C atoi() ?
Well, as the C library function atoi() works on C strings and produces a C
integer, probably there's probably not one that's "identical". You
probably won't get overflow errors, either (i.e., you can parse the string
"876439826926923469238696" into an integer and have it work!).
However, if you're looking for a Lisp function that converts a Lisp string
to a Lisp integer, you might want to have a look at the function
parse-integer. It's much more versatile than the C function, allowing a
radix to be specified, skipping whitespace before and after the string,
allowing better error checking - all-in-all a quite nice function.
(read-from-string "123") -> the number 123
> There are many ways to do this, for example :
> (read-from-string "123") -> the number 123
it's better to use parse-integer if parsing an integer is what you
want to do. Some lisps, at least lispworks, will execute parse-integer
significantly faster than read-from-string.
Note: please use only 7-bit characters in your message header
and body when posting to a Usenet discussion group (or at least
an English-language one).
>I'm novice in LISP.
>I want a function which transform a string into number (Yea same as atoi()
You mean, you want a Lisp function that can reformat your hard drive or
make demons fly out of your nose if the number being converted cannot
be represented in the target type? :)
atoi is a dangerous function that should only be used when the
text of the number has been lexically verified to be convertible
without error, because the function has no means of reporting error,
and invokes undefined behavior if the integer is too large or too small.
Instead of atoi and atof, you should consider using the functions
strtol, strtoul and strtod.