From: Suzanne Mercer Paley
Subject: making strings with make-array
Date: 
Message-ID: <PALEY.95Feb2191523@Cayucos.ai.sri.com>
I want to be able to make a string by calling make-array (this is so
that I can actually make a displaced array and keep a pointer to the
middle of a string).

I can do it by calling (make-array n :element-type 'string-char), but
I notice from my reading of CLtL2 that the string-char type has
actually been officially discontinued (despite its continued presence
in my current version of lisp, Lucid 4.1.something).  Instead, we get
the rather cryptic statement:

"... to redefine the type string to be the union of one or more
specialized vector types, the types of whose elements are subtypes of
the type character."

I tried making a string by calling (make-array n :element-type
'character), but instead of a string, I got a
#<Simple-Vector T 25 2CE085E>, which neither looks like a string, nor
does it appear to be coercible to a string.  Is there a way to do what
I want while remaining within the new spec?  (I'd be happy to use
string-chars, which work, but I'm worried about them disappearing from
future versions of lisp.)

Please respond to me in email, as I am not a regular reader of this
group.

Thanks,
Suzanne
--
Suzanne Paley
·····@ai.sri.com
SRI International

From: Philip L. Stubblefield
Subject: Re: making strings with make-array
Date: 
Message-ID: <phil-0302951317150001@mac-203.rpal.rockwell.com>
In article <··················@Cayucos.ai.sri.com>, ·····@ai.sri.com
(Suzanne Mercer Paley) wrote:

> I want to be able to make a string by calling make-array (this is so
> that I can actually make a displaced array and keep a pointer to the
> middle of a string).
> 
> I can do it by calling (make-array n :element-type 'string-char), but
> I notice from my reading of CLtL2 that the string-char type has
> actually been officially discontinued (despite its continued presence
> in my current version of lisp, Lucid 4.1.something).  [...]
>
> [...]  Is there a way to do what
> I want while remaining within the new spec?  (I'd be happy to use
> string-chars, which work, but I'm worried about them disappearing from
> future versions of lisp.)

Right, the problem is that different implementations still have different
types for string characters.  An effective hack is something like the
following:

   (MAKE-ARRAY <length>
               :ELEMENT-TYPE '#.(ARRAY-ELEMENT-TYPE "")
               ...)

At read time, the #. reader macro will determine the appropriate element
type for a string and substitute it into your code.  Note that the quote
is important; without it, you'll wind up with a code fragment something
like :ELEMENT-TYPE LISP:STRING-CHAR, which will cause the symbol
LISP:STRING-CHAR to be evaluated at run time.

For example, here's a utility routine from one of our systems.  I'm sure
you can adapt it to your needs.  Good luck!


;;; Creates and returns an adjustable string with a fill pointer.  If
;;; the value of INITIAL-CONTENTS is a string, then that value is used
;;; to initialize the new string, leaving the fill-pointer at the end of
;;; the string.  Otherwise, the string is initialized to a length equal
;;; to the value of INITIAL-SIZE, which must be a non-negative integer,
;;; and the fill-pointer is set to zero.  (The #. hack is useful while
;;; implementations are switching to ANSI character types.)

(defvar *buffer-default-initial-size* 64
  "The initial size of a buffer unless otherwise specified.")

(defun make-buffer-string (&key initial-size initial-contents)
  (if (null initial-contents)
      (make-array (or initial-size *buffer-default-initial-size*)
                  :element-type '#.(array-element-type "")
                  :fill-pointer 0
                  :adjustable t)
      (let ((initial-size (length initial-contents)))
        (make-array initial-size
                    :element-type '#.(array-element-type "")
                    :fill-pointer initial-size
                    :adjustable t
                    :initial-contents initial-contents))))



Philip L. Stubblefield                                      415/325-7165
Rockwell Palo Alto Laboratory                     ····@rpal.rockwell.com
From: Lawrence G. Mayka
Subject: Re: making strings with make-array
Date: 
Message-ID: <LGM.95Feb5200359@polaris.ih.att.com>
In article <··················@Cayucos.ai.sri.com> ·····@ai.sri.com (Suzanne Mercer Paley) writes:

   I tried making a string by calling (make-array n :element-type
   'character), but instead of a string, I got a
   #<Simple-Vector T 25 2CE085E>, which neither looks like a string, nor
   does it appear to be coercible to a string.  Is there a way to do what
   I want while remaining within the new spec?  (I'd be happy to use
   string-chars, which work, but I'm worried about them disappearing from
   future versions of lisp.)

CLtL2 certainly provides a character type that is guaranteed to result
in a string: BASE-CHARACTER.  (I believe that in ANSI Common Lisp,
this has been renamed to BASE-CHAR.)

However, CLtL2 is ambiguous about whether a (VECTOR CHARACTER) must be
a STRING.  The closest it comes to such an assertion is a clause in
the description of MAKE-STRING: "If :ELEMENT-TYPE is omitted, the type
CHARACTER is the default."  I believe that ANSI Common Lisp settles
the matter more clearly, that a (VECTOR CHARACTER) must be a STRING.
--
        Lawrence G. Mayka
        AT&T Bell Laboratories
        ···@ieain.att.com

Standard disclaimer.