From: Amos Waterland
Subject: How to convert a string to a number?
Date:
Message-ID: <3AC2CDB7.652556BB@ou.edu>
All:
I have a perhaps clueless question: When using CLISP, how can one
convert a string to an integer? I have found references to a
(string-to-number s) function, but this appears to exist only in EMACS
Lisp. The following script depicts what I am attempting to do; any
advice will be greatly appreciated.
Amos Waterland
Script started on Wed Mar 28 23:50:02 2001
csAI% cat parse-int.lisp
#!/usr/local/bin/clisp
(print *args*)
(print (car *args*))
(print (+ 1 (car *args*)))
csAI% ./parse-int.lisp 1 2 3
("1" "2" "3")
"1"
*** - argument to + should be a number: "1"
csAI% exit
exit
Script done on Wed Mar 28 23:50:08 2001
"Amos Waterland" <·······@ou.edu> wrote in message
······················@ou.edu...
> All:
>
> I have a perhaps clueless question: When using CLISP, how can one
> convert a string to an integer? I have found references to a
> (string-to-number s) function, but this appears to exist only in EMACS
> Lisp. The following script depicts what I am attempting to do; any
> advice will be greatly appreciated.
>
> Amos Waterland
>
>
> Script started on Wed Mar 28 23:50:02 2001
> csAI% cat parse-int.lisp
> #!/usr/local/bin/clisp
>
> (print *args*)
>
> (print (car *args*))
>
> (print (+ 1 (car *args*)))
> csAI% ./parse-int.lisp 1 2 3
>
> ("1" "2" "3")
> "1"
> *** - argument to + should be a number: "1"
>
> csAI% exit
> exit
>
> Script done on Wed Mar 28 23:50:08 2001
CL-USER 3 : 1 > (mapcar #'read-from-string '("1" "2" "3"))
(1 2 3)
Geoff
"Paul Tarvydas" <·········@tscontrols.com> writes:
> > In Common Lisp, use the function PARSE-INTEGER.
>
> Is there something similar for float?
Yes. READ-FROM-STRING. Heh. Ok, not really what you wanted. But it works.
You should bind *READ-EVAL* to NIL to avoid trojan horses.
And I usually do:
(EVERY #'(LAMBDA (C) (FIND C "0123456789.+-esfdl" :TEST #'CHAR-EQUAL)) STRING)
and
(FLOATP RESULT)
just to be sure.
If you really need speed, though, there's probably a hidden
system-level primitive that is more specific in most implementations;
ask your vendor.
And I think something like the CMU respository has a portable PARSE-FLOAT.
It's possible to write--it's just a pain.
Amos Waterland <·······@ou.edu> wrote:
> When using CLISP, how can one convert a string to an integer?
parse-integer.
--
Georges Ko (Taipei, Taiwan) 2001-03-29 ···@gko.net / ICQ: 8719684
Cycle 78, an 18 (Xin-Si), mois 3 (Ren-Chen), jour 5 (Xin-Mao)