I'm new at this lisp thing... How do you convert a string to a
number, like atoi or atof in C? I use
(defun aton (str)
(when (> (length str) 0)
(read (make-string-input-stream str))))
but there must be a function somewhere that does this. (Steele's book
sure is big :-( ).
john
···········@ucdavis.edu
In article <··········@ucdavis.edu>, ····@josquin.ucdavis.edu (John Gillespie) writes:
|> I'm new at this lisp thing... How do you convert a string to a
^^^^^^^^^^^^^^^
that is truly funny!
|> number, like atoi or atof in C? I use
|>
|> (defun aton (str)
|> (when (> (length str) 0)
|> (read (make-string-input-stream str))))
|>
|> but there must be a function somewhere that does this. (Steele's book
|> sure is big :-( ). ^^^^^^^^^^^^^
^^^^^^^^^^^
so is this :-)
|> john
|> ···········@ucdavis.edu
try read-from-string. this will read whatever object is contained in
its string argument. unlike C, where you have a zillion :-( functions
to read objects from strings, lisp has only one such function!
david
In article <··········@ucdavis.edu>, ····@josquin.ucdavis.edu (John Gillespie) writes:
> I'm new at this lisp thing... How do you convert a string to a
> number, like atoi or atof in C? I use
>
> (defun aton (str)
> (when (> (length str) 0)
> (read (make-string-input-stream str))))
>
> but there must be a function somewhere that does this. (Steele's book
> sure is big :-( ).
>
(setq out (read-from-string <str>))
Also, check *read-default-float-format* - if you want true double float
precision (without using IEEE coersion) set this global var to be 'double-float
From Martin Glanvill
--===**| "Arm Powered" Dodo Breeder's Association of New Zealand Inc. |**===--
e-mail : ···@hoiho.math.waikato.ac.nz
********
snail-mail : c/- Dept Mathematics & Statistics *****/|*
University of Waikato ****/#|*
PO Box 3105 ***/__|*
Hamilton 2020 **/~~~|*
New Zealand ********
A bird in the hand is safer than two overhead.
In article <··········@ucdavis.edu> ····@josquin.ucdavis.edu (John Gillespie) writes:
>I'm new at this lisp thing... How do you convert a string to a
>number, like atoi or atof in C? I use
The CL equivalent of atoi() is PARSE-INTEGER. There is no direct analogue
of atof(), so you have to use READ-FROM-STRING and appropriate error
checking (including handling the condition that will be raised if EOF is
encountered in the middle of an object, due to mismatched parentheses).
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar