Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Adding a charcter to the end of a string.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
beoneel  
View profile  
 More options Oct 31 2000, 1:00 pm
Newsgroups: comp.lang.lisp
From: beon...@my-deja.com
Date: Tue, 31 Oct 2000 17:52:23 GMT
Local: Tues, Oct 31 2000 12:52 pm
Subject: Adding a charcter to the end of a string.
Hi,
  Along the lines of "this can't be as hard as I'm making it" how does one
concate a character to a string?  I would like to do  something like:

(setf a (make-string 100))
(blah a #\b)
(blah a #\b)
(blah a #\c)

and then have a contain the string "bbc"

I can see how to do it with CONCATENATE but then I end up with

(setf a (make-string 100))
(setf a (concatenate 'string a (make-string 1 :initial-element #\b)))
(setf a (concatenate 'string a (make-string 1 :initial-element #\b)))
(setf a (concatenate 'string a (make-string 1 :initial-element #\c)))

but this just seems too hideous.  I also see how one could do it with aref

(setf a (make-string 100))
(setf (aref a 1) #\b)
(setf (aref a 2) #\b)
(setf (aref a 3) #\c)

but I would like not to have to keep track of indices and I'd like not to
worry about that 101st element.

Thanks ever so much.  It's been a long time and I didn't work with strings
last time I worked with lisp.

cheers

bruce

Sent via Deja.com http://www.deja.com/
Before you buy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Oct 31 2000, 1:21 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Tue, 31 Oct 2000 18:21:10 GMT
Local: Tues, Oct 31 2000 1:21 pm
Subject: Re: Adding a charcter to the end of a string.

In article <8tn0sj$30...@nnrp1.deja.com>,  <beon...@my-deja.com> wrote:
>Hi,
>  Along the lines of "this can't be as hard as I'm making it" how does one
>concate a character to a string?  I would like to do  something like:

>(setf a (make-string 100))
>(blah a #\b)
>(blah a #\b)
>(blah a #\c)

>and then have a contain the string "bbc"

Use a vector with a fill pointer, and use vector-push:

(setf a (make-array 100 :type 'character :fill-pointer 0))
(vector-push a #\b)
(vector-push a #\b)
(vector-push a #\c)

If you want it to be able to grow past 100 characters, use :extendable t
when making the array, and use vector-push-extend instead.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SRS  
View profile  
 More options Oct 31 2000, 1:50 pm
Newsgroups: comp.lang.lisp
From: SRS <sr...@my-deja.com>
Date: Tue, 31 Oct 2000 18:36:51 GMT
Local: Tues, Oct 31 2000 1:36 pm
Subject: Re: Adding a charcter to the end of a string.
In article <8tn0sj$30...@nnrp1.deja.com>,

It does indeed, but you do not have to use make-string in this
situation. just coerce the chars you want in to a string using the
function string, and concatenate then to the variable a:

    (setf (concatenate 'string a (string #\a)))
    (setf (concatenate 'string a (string #\b)))
    (setf (concatenate 'string a (string #\c)))

You could also do

    (setf a (format nil "~Aa" a))
    ...and so on

(But if you want a dynamically resizable sequence of letters, why not
use a list, and then coerce it into a string when you need to print it?)

-- SRS

Sent via Deja.com http://www.deja.com/
Before you buy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Clonts  
View profile  
 More options Oct 31 2000, 3:20 pm
Newsgroups: comp.lang.lisp
From: John Clonts <joh...@my-deja.com>
Date: Tue, 31 Oct 2000 20:09:01 GMT
Local: Tues, Oct 31 2000 3:09 pm
Subject: Re: Adding a charcter to the end of a string.
In article <8tn3g2$5g...@nnrp1.deja.com>,
  SRS <sr...@my-deja.com> wrote:

> In article <8tn0sj$30...@nnrp1.deja.com>,
>   beon...@my-deja.com wrote:
[snip]

> (But if you want a dynamically resizable sequence of letters, why not
> use a list, and then coerce it into a string when you need to print
it?)

By coerce I guess you mean

   (concatenate 'string '(#\a #\b #c)) ==> "abc"

But I have a different question:  How to "coerce" a list of symbols to
append them into a string:

   (setf aa '(a bc de f))
   (list-of-symbols-to-string aa)  ==> "abcdef"

This is how I did it:
   (defun list-of-symbols-to-string (list)
      (apply #'concat-strings (mapcar #'string list)))

   (defun concat-strings (&rest r)
      (eval (append '(concatenate 'string ) r)))

But I really wanted to do without the eval, rather something like:
   (defun concat-strings (&rest r)
      (concatenate 'string @r))

Thanks,
John

Sent via Deja.com http://www.deja.com/
Before you buy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Oct 31 2000, 3:32 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 31 Oct 2000 20:32:01 +0000
Local: Tues, Oct 31 2000 3:32 pm
Subject: Re: Adding a charcter to the end of a string.
* John Clonts <joh...@my-deja.com>
| But I have a different question:  How to "coerce" a list of symbols
| to append them into a string:

  By not getting into this fix to begin with.  (Why are they symbols?)

|    (setf aa '(a bc de f))
|    (list-of-symbols-to-string aa)  ==> "abcdef"
|
| This is how I did it:
|    (defun list-of-symbols-to-string (list)
|       (apply #'concat-strings (mapcar #'string list)))
|
|    (defun concat-strings (&rest r)
|       (eval (append '(concatenate 'string ) r)))
|
| But I really wanted to do without the eval, rather something like:
|    (defun concat-strings (&rest r)
|       (concatenate 'string @r))

(apply #'concatenate 'string (mapcar #'string list))

  If you have a Scheme background (where apply takes two arguments),
  you could have consed 'string onto the list of arguments, too.

#:Erik
--
  Does anyone remember where I parked Air Force One?
                                   -- George W. Bush


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Oct 31 2000, 4:44 pm
Newsgroups: comp.lang.lisp
From: Rainer Joswig <jos...@corporate-world.lisp.de>
Date: Tue, 31 Oct 2000 22:43:45 +0100
Local: Tues, Oct 31 2000 4:43 pm
Subject: Re: Adding a charcter to the end of a string.
In article <8tn3g2$5g...@nnrp1.deja.com>, SRS <sr...@my-deja.com>
wrote:

> It does indeed, but you do not have to use make-string in this
> situation. just coerce the chars you want in to a string using the
> function string, and concatenate then to the variable a:

>     (setf (concatenate 'string a (string #\a)))
>     (setf (concatenate 'string a (string #\b)))
>     (setf (concatenate 'string a (string #\c)))

>     (setf (concatenate 'string a (string #\a)))

Which would be really:

(setf a (concatenate 'string a (string #\a)))

Also one does not concatenate the string to a variable.
You are concatenating the value of A (-> a string) and the
value of (STRING #\a) (-> also a string) to a new (!!) string.

Common Lisp also has the idea of adjustable array (which
can change the size) and arrays with a fill-pointer.
Adding elements to an array with a FILL-POINTER
is done via VECTOR-PUSH. If you want to use the feature
of an adjustable array, VECTOR-PUSH-EXTEND is your
friend:

? (setf a (make-array 10
                      :element-type 'character
                      :adjustable t
                      :fill-pointer 0))
""

? (vector-push-extend #\a a)
0

? a
"a"

? (vector-push-extend #\b a)
1

? a
"ab"
? (vector-push-extend #\c a)
2

? a
"abc"

Another way cons up strings is to use a special stream:

(with-output-to-string (stream)
  (princ #\a stream)
  (princ #\b stream)
  (princ #\c stream))

or

(with-output-to-string (*standard-output*)
  (princ #\a)
  (princ #\b)
  (princ #\c))

--
Rainer Joswig, Hamburg, Germany
Email: mailto:jos...@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas A. Russ  
View profile  
 More options Oct 31 2000, 6:11 pm
Newsgroups: comp.lang.lisp
From: t...@sevak.isi.edu (Thomas A. Russ)
Date: 31 Oct 2000 13:43:58 -0800
Local: Tues, Oct 31 2000 4:43 pm
Subject: Re: Adding a charcter to the end of a string.

John Clonts <joh...@my-deja.com> writes:
> But I have a different question:  How to "coerce" a list of symbols to
> append them into a string:

A general solution could be written using format:

(defun list-of-whatever-to-string (list)
  (format nil "~{~A~}" list)
  )

Hmmm.  By an amazing coincidence the body of the function happens to be
the same length as its name.

Note:  Depending on how you want the output to appear in the case of
mixed case symbol names, different packages, etc. you might want to use
~S instead of ~A.

--
Thomas A. Russ,  USC/Information Sciences Institute          t...@isi.edu    


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Scheme's "apply" [was: Re: Adding a charcter ... ]" by Rob Warnock
Rob Warnock  
View profile  
 More options Oct 31 2000, 11:57 pm
Newsgroups: comp.lang.lisp
From: r...@rigden.engr.sgi.com (Rob Warnock)
Date: 1 Nov 2000 04:57:21 GMT
Local: Tues, Oct 31 2000 11:57 pm
Subject: Scheme's "apply" [was: Re: Adding a charcter ... ]
Erik Naggum  <e...@naggum.net> wrote:
+---------------
| (apply #'concatenate 'string (mapcar #'string list))
|
|   If you have a Scheme background (where apply takes two arguments),
|   you could have consed 'string onto the list of arguments, too.
+---------------

Erik, just as Lisp has evolved beyond the outdated concepts of
"interpreter/slow/etc." and Common Lisp has evolved beyond CLtL1,
Scheme too has made *some* progress over its life, and in fact has
had multi-arg "apply" since at least 1986 with the release of R3RS.

Granted, in R3RS and R4RS the two-arg form was called an "essential
procedure" and the multi-arg form was just a "procedure", but by the
time of the IEEE Scheme Standard in 1991 (and in R5RS today), this
distinction had vanished:

        (apply proc arg1 ... args)

        Proc must be a procedure and args must be a list. Calls proc
        with the elements of the list (append (list arg1 ...) args)
        as the actual arguments.

For example:

        > (apply vector 1 2 3 '(4 5 6))
        #(1 2 3 4 5 6)
        > (apply string-append (map string '(#\a #\c #\e) '(#\b #\d #\f)))
        "abcdef"
        >

-Rob

-----
Rob Warnock, 31-2-510           r...@sgi.com
Network Engineering             http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.          Phone: 650-933-1673
1600 Amphitheatre Pkwy.         PP-ASEL-IA
Mountain View, CA  94043


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Adding a charcter to the end of a string." by John Clonts
John Clonts  
View profile  
 More options Nov 1 2000, 12:19 am
Newsgroups: comp.lang.lisp
From: John Clonts <jclo...@mastnet.net>
Date: Wed, 01 Nov 2000 05:16:38 GMT
Local: Wed, Nov 1 2000 12:16 am
Subject: Re: Adding a charcter to the end of a string.

Erik Naggum wrote:

> * John Clonts <joh...@my-deja.com>
> | But I have a different question:  How to "coerce" a list of symbols
> | to append them into a string:

>   By not getting into this fix to begin with.  (Why are they symbols?)

Hmm, good point.

Perfect, thanks.  I guess I now better understand "spreadable argument
list designator".

Cheers,
John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
beoneel  
View profile  
 More options Nov 1 2000, 4:00 am
Newsgroups: comp.lang.lisp
From: beon...@my-deja.com
Date: Wed, 01 Nov 2000 08:46:19 GMT
Local: Wed, Nov 1 2000 3:46 am
Subject: Re: Adding a charcter to the end of a string.
Thanks to everyone!

cheers

bruce

Sent via Deja.com http://www.deja.com/
Before you buy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Scheme's "apply" [was: Re: Adding a charcter ... ]" by Erik Naggum
Erik Naggum  
View profile  
 More options Nov 1 2000, 10:05 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 01 Nov 2000 14:10:18 +0000
Local: Wed, Nov 1 2000 9:10 am
Subject: Re: Scheme's "apply" [was: Re: Adding a charcter ... ]
* Rob Warnock
| Erik, just as Lisp has evolved beyond the outdated concepts of
| "interpreter/slow/etc." and Common Lisp has evolved beyond CLtL1,
| Scheme too has made *some* progress over its life, and in fact has
| had multi-arg "apply" since at least 1986 with the release of R3RS.

  What?  You mean it's time for _me_ to evolve, too?  I thought I
  could avoid that, but if my .project ("immortality in our lifetime")
  has any hopes, I'd better get used to it.

  Seriously, though, I have always disliked the separation between
  essential and non-essential procedueres in Scheme, so if it has
  vanished as a whole, I'm as delighted as I can be when I don't like
  Scheme to begin with.  If it is only gone for apply, well, it's a
  step in the right direction.

  Thanks for the update.

#:Erik
--
  Does anyone remember where I parked Air Force One?
                                   -- George W. Bush


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Nov 1 2000, 10:18 am
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 01 Nov 2000 10:14:42 -0500
Local: Wed, Nov 1 2000 10:14 am
Subject: Re: Scheme's "apply" [was: Re: Adding a charcter ... ]

All true, but Scheme does not have arrays with fill pointers. :)
Which is what is most useful in this case.

Cheers

--
Marco Antoniotti =============================================================
NYU Bioinformatics Group                         tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA                          http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
                              Paul Graham, ANSI Common Lisp


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Warnock  
View profile  
 More options Nov 5 2000, 11:51 pm
Newsgroups: comp.lang.lisp
From: r...@rigden.engr.sgi.com (Rob Warnock)
Date: 6 Nov 2000 04:51:17 GMT
Local: Sun, Nov 5 2000 11:51 pm
Subject: Re: Scheme's "apply" [was: Re: Adding a charcter ... ]
[Sorry to have taken so long for this reply...]

Erik Naggum  <e...@naggum.net> wrote:
+---------------
| * Rob Warnock
| | Scheme... has had had multi-arg "apply" since at least 1986... R3RS.
...
|   Seriously, though, I have always disliked the separation between
|   essential and non-essential procedueres in Scheme, so if it has
|   vanished as a whole, I'm as delighted as I can be when I don't like
|   Scheme to begin with.  If it is only gone for apply, well, it's a
|   step in the right direction.
+---------------

Well, I've got some good news, and some bad news... ;-}

IEEE Standard Scheme [1991] did indeed erase the "essential/non-essential"
distinction completely, but... as of R5RS "it's BAAAAaaaack", in the form
of "primitive", "library", and "optional" syntax & procedures:

        The report is now a superset of the IEEE standard for Scheme [so]...
        The classification of features as essential or inessential has
        been removed. There are now three classes of built-in procedures:
        primitive, library, and optional. The optional procedures are
        load, with-input-from-file, with-output-to-file, transcript-on,
        transcript-off, and interaction-environment, and - and / with
        more than two arguments. None of these are in the IEEE standard.

One might reasonably question "load" being optional, but since "eval" is
now required [since everything in R5RS that is not "optional" is required],
"load" can be synthesized from other primitives.

Likewise, "with-input-from-file" & "with-output-to-file" can be emulated
with wrappers [and in any event are dangerous in the presence of errors,
since R5RS makes no guarantees about file closure on error -- "If an
escape procedure is used to escape from the continuation of these
procedures, their behavior is implementation dependent" -- whereas in
one's own implementation one can at least choose the desired behavior,
using "dynamic-wind" (except: see last item below!)].

As for the primitive/library distinction:

        1.3.1 Primitive, library, and optional features
        ...
        To aid in understanding and implementing Scheme, some features
        are marked as library. These can be easily implemented in terms
        of the other, primitive, features. They are redundant in the
        strict sense of the word, but they capture common patterns of
        usage, and are therefore provided as convenient abbreviations.

Since R5RS has "syntax-rules"-style hygenic macros as a required
primitive feature, macros can be used to implement such "library
syntax" as cond/case/and/or/let/let*/letrec/begin/do/delay, &c.

Finally, as mentioned above, "dynamic-wind" is now required, which at
least sets the stage for decent exception handling, but unfortunately
errors are *not* yet specified in terms of callinng a [top-level]
continuation, so there's still no guarantee that an error will even
invoke the "out" limb of a "dynamic-wind" at all!!  (*sigh*)

-Rob

-----
Rob Warnock, 31-2-510           r...@sgi.com
Network Engineering             http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.          Phone: 650-933-1673
1600 Amphitheatre Pkwy.         PP-ASEL-IA
Mountain View, CA  94043


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kent M Pitman  
View profile  
 More options Nov 6 2000, 1:18 am
Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: Mon, 6 Nov 2000 06:12:58 GMT
Local: Mon, Nov 6 2000 1:12 am
Subject: Re: Scheme's "apply" [was: Re: Adding a charcter ... ]

r...@rigden.engr.sgi.com (Rob Warnock) writes:
> Likewise, "with-input-from-file" & "with-output-to-file" can be emulated
> with wrappers [and in any event are dangerous in the presence of errors,
> since R5RS makes no guarantees about file closure on error -- "If an
> escape procedure is used to escape from the continuation of these
> procedures, their behavior is implementation dependent" -- whereas in
> one's own implementation one can at least choose the desired behavior,
> using "dynamic-wind" (except: see last item below!)].

No, this is orthogonal to dynamic-wind.  Dynamic-wind is how you implement
process switching, but this isn't the problem for with-input-from-file that
Scheme has.  The problem there is functions that return more than once,
and in those cases only the GC knows whether someone is holding a pointer
to something that might be reinvoked.  Dynamic wind doesn't run
unwind-protects like with-input-from-file would set up, but that doesn't
mean there isn't a crisis if other kinds of escape procedures are run.
A consequence is that files only get closed when the gc is good and ready,
and can't be properly closed [as a CL user would expect] reliably upon the
point of escape.

> Finally, as mentioned above, "dynamic-wind" is now required, which at
> least sets the stage for decent exception handling, but unfortunately
> errors are *not* yet specified in terms of callinng a [top-level]
> continuation, so there's still no guarantee that an error will even
> invoke the "out" limb of a "dynamic-wind" at all!!  (*sigh*)

Uh, I'm not clear on this but would have to look harder.  It's not obvious
to me why dynamic-wind can't cooperate neatly with other unwinds, unless
it's just a typo in the spec [which I don't have handy as I write this].

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Boris Schaefer  
View profile  
 More options Nov 7 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Boris Schaefer <bo...@uncommon-sense.net>
Date: 2000/11/07
Subject: Re: Scheme's "apply" [was: Re: Adding a charcter ... ]
Kent M Pitman <pit...@world.std.com> writes:

| No, this is orthogonal to dynamic-wind.  Dynamic-wind is how you
| implement process switching, but this isn't the problem for
| with-input-from-file that Scheme has.  The problem there is
| functions that return more than once, and in those cases only the GC
| knows whether someone is holding a pointer to something that might
| be reinvoked.  Dynamic wind doesn't run unwind-protects like
| with-input-from-file would set up, but that doesn't mean there isn't
| a crisis if other kinds of escape procedures are run.  A consequence
| is that files only get closed when the gc is good and ready, and
| can't be properly closed [as a CL user would expect] reliably upon
| the point of escape.

Do I understand it correctly that you mean something like:

  (define continuation #f)

  (with-input-from-file
      "foo"
    (lambda ()
      (if (call-with-current-continuation
           (lambda (cc)
             (set! continuation cc)
             #t))
          'do-nothing
          (read-char))))

  (continuation #f)

or are you talking about something else?

--
bo...@uncommon-sense.net - <http://www.uncommon-sense.net/>

I think a relationship is like a shark.  It has to constantly move forward
or it dies.  Well, what we have on our hands here is a dead shark.
                -- Woody Allen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »