From: Nicolas Troquard
Subject: number2string
Date: 
Message-ID: <3E89622F.94FA28A0@emi.u-bordeaux.fr>
A tiny question :
Is there any function (number2string 8) => "8" that exists?

thanks

nt

From: Kaz Kylheku
Subject: Re: number2string
Date: 
Message-ID: <cf333042.0304011327.7dd7166c@posting.google.com>
Nicolas Troquard <········@emi.u-bordeaux.fr> wrote in message news:<·················@emi.u-bordeaux.fr>...
> A tiny question :
> Is there any function (number2string 8) => "8" that exists?

You can't do that in Lisp, but most Lisps let you call a C function
using their FFI syntax, such as:

;; CLISP
(def-c-call-out itoa
   (:arguments (arg integer)
               (base integer))
   (:return-type c-string))


(itoa 8 10) ==> "8"
                
> thanks

No problem.
From: Franz Kafka
Subject: Re: number2string
Date: 
Message-ID: <b3b6b110.0304012114.1978f467@posting.google.com>
···@ashi.footprints.net (Kaz Kylheku) wrote in message news:<····························@posting.google.com>...
> Nicolas Troquard <········@emi.u-bordeaux.fr> wrote in message news:<·················@emi.u-bordeaux.fr>...
> > A tiny question :
> > Is there any function (number2string 8) => "8" that exists?
> 
> You can't do that in Lisp, but most Lisps let you call a C function
> using their FFI syntax, such as:
> 

you could also try

(defun number2string (number)
  (coerce number 'string) ;; or what ever other datatype you need.
)

or

(defmacro number2string (number)
  `(car (",number")) ;; This should work.
) 

or maybe even
(defmacro number2string (number) `",number")
I'm not sure if this will work. My compiler is DOWN.

I think it shout return
"number"

There are many many ways to do something in LISP. The only reason you
would need to call C. You want to use a C Library function and not rewrite
it into Lisp, or you have some highly optimized C or C + ASM code that you
want to call from Lisp.
From: Gabe Garza
Subject: Re: number2string
Date: 
Message-ID: <87llytlbao.fsf@ix.netcom.com>
·································@hotmail.com (Franz Kafka) writes:


> you could also try
> 
> (defun number2string (number)
>   (coerce number 'string) ;; or what ever other datatype you need.
> )
> 
> or
> 
> (defmacro number2string (number)
>   `(car (",number")) ;; This should work.
> ) 
> 
> or maybe even
> (defmacro number2string (number) `",number")

Very elegant.  You can also do:

(defun number2string (number)
  (the string number))

or

(defun number2string (number)
  (setf (type-of number) 'string)
  number)

But if efficiency is a concern, this would be best:

(defun number2string (number) 
  (declare (optimize (speed 3))  
           (type string number))
  number)

Gabe Garza
From: Matthew Danish
Subject: Re: number2string
Date: 
Message-ID: <20030402004048.C13181@lain.cheme.cmu.edu>
On Wed, Apr 02, 2003 at 05:26:43AM +0000, Gabe Garza wrote:
> ·································@hotmail.com (Franz Kafka) writes:
> > you could also try
> > 
> > (defun number2string (number)
> >   (coerce number 'string) ;; or what ever other datatype you need.
> > )
> > 
> > or
> > 
> > (defmacro number2string (number)
> >   `(car (",number")) ;; This should work.
> > ) 
> > 
> > or maybe even
> > (defmacro number2string (number) `",number")
> 
> Very elegant.  You can also do:
> 
> (defun number2string (number)
>   (the string number))
> 
> or
> 
> (defun number2string (number)
>   (setf (type-of number) 'string)
>   number)
> 
> But if efficiency is a concern, this would be best:
> 
> (defun number2string (number) 
>   (declare (optimize (speed 3))  
>            (type string number))
>   number)

NNTP-Posting-Date: Wed, 02 Apr 2003 00:26:43 EST                                 

STOPPPPPPPPPPPPPPPPPP!!!!!!!!!!!!!!!!!!!!!! =)


P.S.

I like

(defun number2string (number)
  (change-class number 'string))

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Peter Seibel
Subject: Re: number2string
Date: 
Message-ID: <m3vfxxu4nr.fsf@localhost.localdomain>
···@ashi.footprints.net (Kaz Kylheku) writes:

> Nicolas Troquard <········@emi.u-bordeaux.fr> wrote in message news:<·················@emi.u-bordeaux.fr>...
> > A tiny question :
> > Is there any function (number2string 8) => "8" that exists?
> 
> You can't do that in Lisp, but most Lisps let you call a C function
> using their FFI syntax, such as:
> 
> ;; CLISP
> (def-c-call-out itoa
>    (:arguments (arg integer)
>                (base integer))
>    (:return-type c-string))
> 
> 
> (itoa 8 10) ==> "8"
>                 
> > thanks
> 
> No problem.

Heh. (I hope future Googlers note the date of this post before they
write off Lisp as hopelessly impractical.)

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Coby Beck
Subject: Re: number2string
Date: 
Message-ID: <b6deu3$gbt$1@otis.netspace.net.au>
"Peter Seibel" <·····@javamonkey.com> wrote in message
···················@localhost.localdomain...
> ···@ashi.footprints.net (Kaz Kylheku) writes:
>
> > Nicolas Troquard <········@emi.u-bordeaux.fr> wrote in message
news:<·················@emi.u-bordeaux.fr>...
> > > A tiny question :
> > > Is there any function (number2string 8) => "8" that exists?
> >
> > You can't do that in Lisp, but most Lisps let you call a C function
> > using their FFI syntax, such as:
> >
> > ;; CLISP
> > (def-c-call-out itoa
> >    (:arguments (arg integer)
> >                (base integer))
> >    (:return-type c-string))
> >
> >
> > (itoa 8 10) ==> "8"
> >
> > > thanks
> >
> > No problem.
>
> Heh. (I hope future Googlers note the date of this post before they
> write off Lisp as hopelessly impractical.)
>

Ah.  Sorry I didn't get it, but you see it is well into April 2 already
where I am...

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Coby Beck
Subject: Re: number2string
Date: 
Message-ID: <b6dedn$gag$1@otis.netspace.net.au>
"Kaz Kylheku" <···@ashi.footprints.net> wrote in message
·································@posting.google.com...
> Nicolas Troquard <········@emi.u-bordeaux.fr> wrote in message
news:<·················@emi.u-bordeaux.fr>...
> > A tiny question :
> > Is there any function (number2string 8) => "8" that exists?
>
> You can't do that in Lisp, but most Lisps let you call a C function
> using their FFI syntax, such as:

Hi Kaz,

I don't get your joke, if it is a joke or your subtle point, if it is a
subtle point.

Common Lisp can do the above with WRITE-TO-STRING, PRINC-TO-STRING,
PRIN1-TO-STRING and FORMAT with a large variety of directives.  (I know you
know this very well, but I don't want your post archived with no answer)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Nils Goesche
Subject: Re: number2string
Date: 
Message-ID: <87wuic6h54.fsf@darkstar.cartan>
"Coby Beck" <·····@mercury.bc.ca> writes:

> I know you know this very well, but I don't want your post
> archived with no answer

Always remember: Over here in comp.lang.lisp, we post for eternity

;-)

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Nicolas Troquard
Subject: Re: number2string
Date: 
Message-ID: <3E89C0B7.1F28D364@emi.u-bordeaux.fr>
thanks for all your responses.
i did try with (format T "~D" 78) and not with NIL, it was a mistake.
Paul F. proposed me by mail the function PRINC-TO-STRING.
I note WRITE-TO-STRING is good too.
Thanks for your responses.

Regards

nico
From: Coby Beck
Subject: Re: number2string
Date: 
Message-ID: <b6bt0f$ggt$1@otis.netspace.net.au>
"Nicolas Troquard" <········@emi.u-bordeaux.fr> wrote in message
······················@emi.u-bordeaux.fr...
> A tiny question :
> Is there any function (number2string 8) => "8" that exists?

As with most simple questions, there are no simple answers!  That said, try
write-to-string.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Adam Warner
Subject: Re: number2string
Date: 
Message-ID: <pan.2003.04.01.10.46.11.38748@consulting.net.nz>
Hi Nicolas Troquard,

> A tiny question :
> Is there any function (number2string 8) => "8" that exists?

format does this kind of stuff:

* (format nil "~A" 8)

"8"

Format can do a lot more as well.

Speed tip for constant data: Use the #. reader macro to evaluate the
format function at read time. Here's a CLISP example:

[6]> (defun test () (dotimes (i 1000000) (length (format nil "~A" 8))))
test
[7]> (compile 'test)
test ;
nil ;
nil
[8]> (time (test))

Real time: 7.85763 sec.
Run time: 7.85 sec.
Space: 632000000 Bytes
GC: 1205, GC time: 2.81 sec.
nil
[9]> (defun test () (dotimes (i 1000000) (length #.(format nil "~A" 8))))
test
[10]> (compile 'test)
test ;
nil ;
nil
[11]> (time (test))

Real time: 0.140636 sec.
Run time: 0.14 sec.
Space: 0 Bytes
nil

Check out the difference with CMUCL:
* (defun test () (dotimes (i 1000000) (length (format nil "~A" 8))))

test
* (compile 'test)
; Compiling lambda nil: 
; Compiling Top-Level Form: 

test
nil
nil
* (time (test))
; Compiling lambda nil: 
; Compiling Top-Level Form: 

; Evaluation took:
;   11.18 seconds of real time
;   10.0 seconds of user run time
;   1.13 seconds of system run time
;   [Run times include 5.05 seconds GC run time]
;   368 page faults and
;   760,024,192 bytes consed.
; 
nil
* (defun test () (dotimes (i 1000000) (length #.(format nil "~A" 8)))) 

test
* (compile 'test)                                                     
; Compiling lambda nil: 
; Compiling Top-Level Form: 

test
nil
nil
* (time (test))                                                       
; Compiling lambda nil: 
; Compiling Top-Level Form: 

; Evaluation took:
;   0.0 seconds of real time
;   0.0 seconds of user run time
;   0.0 seconds of system run time
;   0 page faults and
;   0 bytes consed.
; 
nil

Disappointing result in the first CMUCL run but a huge difference with the
second run (it feels instantaneous).

The #. macro is typically really useful when one wants to include strings
with newlines in code without writing "
"

#.(format nil "~%") is just as fast at run time. One would wish a more
intelligent compiler could figure out that (format nil "~%") always
returns "
" without evaluating (format nil "~%") each time.

Regards,
Adam
From: Marco Baringer
Subject: Re: number2string
Date: 
Message-ID: <m2vfxy760c.fsf@bese.it>
"Adam Warner" <······@consulting.net.nz> writes:

> #.(format nil "~%") is just as fast at run time. One would wish a more
> intelligent compiler could figure out that (format nil "~%") always
> returns "
> " without evaluating (format nil "~%") each time.
> 
> Regards,
> Adam

Why not just define ~% as a constant?

(defconstant ~% (format nil "~%"))

saves the (imo) ugly #. and it's even faster (as fast at execution
time, and faster at read time).

for what it's worth i have the following functions in my personal lisp
collection:

(defun ~D (d &optional stream &key mincol padchar)
  (declare (inline))
  (format stream "~v,vD" mincol padchar d))

(defun ~A (object &optional stream)
  (declare (inline))
  (format stream "~A" object))

(defun ~S (object &optional stream)
  (declare (inline))
  (format stream "~S" object)) 

-- 
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
     -Leonard Cohen
From: Matthew Danish
Subject: Re: number2string
Date: 
Message-ID: <20030401111433.B13181@lain.cheme.cmu.edu>
On Tue, Apr 01, 2003 at 02:32:19PM +0200, Marco Baringer wrote:
> Why not just define ~% as a constant?
> (defconstant ~% (format nil "~%"))

* (eql (format nil "~%") (format nil "~%"))

NIL

You can't create a constant with a form that does not always return
values which are the same under EQL, at least, according to the spec.
Implementations such as SBCL will give you trouble with this.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Nils Goesche
Subject: Re: number2string
Date: 
Message-ID: <87u1diw04c.fsf@darkstar.cartan>
Matthew Danish <·······@andrew.cmu.edu> writes:

> On Tue, Apr 01, 2003 at 02:32:19PM +0200, Marco Baringer wrote:
> > Why not just define ~% as a constant?
> > (defconstant ~% (format nil "~%"))
> 
> * (eql (format nil "~%") (format nil "~%"))
> 
> NIL
> 
> You can't create a constant with a form that does not always
> return values which are the same under EQL, at least, according
> to the spec.

Of course you can.  The initial-value can be any object
whatsoever.  And you can even reload a file containing the form
as often as you like:

# A constant defined by defconstant can be redefined with
# defconstant.

Only that:

# The consequences are undefined [...] if the value is not eql to
# the value of initial-value.

And it is quite clear why this is so: Whenever you use the value
of the constant, the compiler can hardcode the value's address
(because redefining it has undefined consequences), or use it
directly if it is an immediate value.  If you reload the constant
definition, any code that relies on the /identity/ of the
constant will behave, well, possibly strangely (unless it is
recompiled again).

But that doesn't mean that implementations should ``give you
trouble�� with this.  I consider it my God-given right to write

  (defconstant +foobar+ "foobar")

whenever I want.  What if I don't use the identity of +FOOBAR+?
Or if the only two functions that use it immediately follow it
and are always recompiled together with the DEFCONSTANT form?

> Implementations such as SBCL will give you trouble with this.

Thank God it is open source so I can switch that off if I ever
happen to use it ;-)

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Kalle Olavi Niemitalo
Subject: Re: number2string
Date: 
Message-ID: <87el4mj8qi.fsf@Astalo.kon.iki.fi>
Marco Baringer <··@bese.it> writes:

> (defun ~A (object &optional stream)
>   (declare (inline))
>   (format stream "~A" object))

Does (declare (inline)) mean something?
I'd expect it to be a no-op, as no function names are listed.
From: Adam Warner
Subject: Re: number2string
Date: 
Message-ID: <pan.2003.04.01.14.59.37.340153@consulting.net.nz>
Hi Marco Baringer,

>> #.(format nil "~%") is just as fast at run time. One would wish a more
>> intelligent compiler could figure out that (format nil "~%") always
>> returns "
>> " without evaluating (format nil "~%") each time.
>> 
>> Regards,
>> Adam
> 
> Why not just define ~% as a constant?
> 
> (defconstant ~% (format nil "~%"))

Thanks for the tip Marco. I'd perhaps avoid those characters in a symbol
name because they could conceivably be defined elsewhere as reader macro
characters (from 2.1.4 Character Syntax Types): ~ is not used in Common
Lisp, and reserved to implementors. $ and % are alphabetic[2] characters,
but are not used in the names of any standard Common Lisp defined names.

Regards,
Adam
From: Kent M Pitman
Subject: Re: number2string
Date: 
Message-ID: <sfwpto6fbv0.fsf@shell01.TheWorld.com>
"Adam Warner" <······@consulting.net.nz> writes:

> Hi Marco Baringer,
> 
> >> #.(format nil "~%") is just as fast at run time. One would wish a more
> >> intelligent compiler could figure out that (format nil "~%") always
> >> returns "
> >> " without evaluating (format nil "~%") each time.
> >> 
> >> Regards,
> >> Adam
> > 
> > Why not just define ~% as a constant?
> > 
> > (defconstant ~% (format nil "~%"))
> 
> Thanks for the tip Marco. I'd perhaps avoid those characters in a symbol
> name because they could conceivably be defined elsewhere as reader macro
> characters (from 2.1.4 Character Syntax Types): ~ is not used in Common
> Lisp, and reserved to implementors. $ and % are alphabetic[2] characters,
> but are not used in the names of any standard Common Lisp defined names.

Note that there's no problem with them existing in symbol names.
You might just have to escape them for the reader.  \~% would work reliably.

(Just clarifying your terminology, since you said "avoid those characters
in a symbol name", which isn't quite right.  You wanted to say "avoid symbols
that have this name because they may be problematic to type without escaping
in some implementations".  You might have meant this.  But it's a common
source of confusion that people think we are creating limitations on what
can be _in_ a symbol, when in fact any string character can be in a symbol
name...)

On the other hand, I always name the above constant +newline+ in observation
of the popular +xxx+ convention for user-defined constants.
From: Joerg Hoehle
Subject: Re: number2string
Date: 
Message-ID: <uu1dfsgnh.fsf@dont.t-systems.UCE.spam.no.com>
Hi,

"Adam Warner" <······@consulting.net.nz> writes:
> > Is there any function (number2string 8) => "8" that exists?
> format does this kind of stuff:
> * (format nil "~A" 8)
I consider it bad style to recommend FORMAT when something much more
specific exists:
	(write-to-string 8)
	(princ-to-string 8)
WRITE-TO-STRING has many, many keywords
(write-to-string 256 :base 16) -> "100"

[268]> (apropos "-to-string")
PRIN1-TO-STRING                            function
PRINC-TO-STRING                            function
WITH-OUTPUT-TO-STRING                      macro
WRITE-TO-STRING                            function
[...]

> Format can do a lot more as well.
Of course, newbies should notice that format is very powerful.

One can hope that the implementation's format compiler reduces (format
nil "~A" x) to (write-to-string x :escape nil) internally. It must not
necessarily do so. Doing so might bloat code, a typical (declare
(optimize speed)) vs. safety trade-off.

> The #. macro is typically really useful when one wants to include strings
> with newlines in code without writing "
> "

I don't like ... "
"
because, when concerned with portability, one never knows whether this
will become a string containing ASCII CR, CRLF or LF.

One could also use (string #\newline) over (format nil "~%").

Did you know that for the last 10 years, GNU's cccp (C preprocessor)
has had problems running on UNIX with #define spanning multiple lines,
using CRLF as line-separator, as typical for source code that passed
through a MS-DOS/MS-Windows box?
#define ......\[CRLF from DOS instead of UNIX LF]
 ....

Regards,
	Jorg Hohle
Telekom/T-Systems Technology Center
From: Rob Warnock
Subject: Re: number2string
Date: 
Message-ID: <dqqdnczC8oQTTg6jXTWc-g@speakeasy.net>
Nicolas Troquard  <········@emi.u-bordeaux.fr> wrote:
+---------------
| Is there any function (number2string 8) => "8" that exists?
+---------------

If you are asking because you are coming from a Scheme background,
then what you probably really wanted was this:

	> (defun number->string (number &optional (radix 10))
		  (format nil "~vr" radix number))

	NUMBER->STRING
	> (number->string (+ 3 5))

	"8"
	> (number->string (expt 2 32))

	"4294967296"
	> (number->string (expt 2 32) 8)

	"40000000000"
	> (number->string (expt 2 32) 16)

	"100000000"
	>

However, unlike Scheme, which requires the radix to be one of 2, 8,
10, or 16, Common Lisp permits any value between 2 and 36 inclusive:

	> (number->string (expt 2 32) 5)

	"32244002423141"
	> (number->string (expt 2 32) 36)

	"1Z141Z4"
	> 


-Rob

-----
Rob Warnock, PP-ASEL-IA		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607