unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: "R. Diez" <rdiezmail-emacs@yahoo.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Surprising behaviour of 'append' with strings
Date: Sun, 06 Nov 2022 13:13:26 +0100	[thread overview]
Message-ID: <87v8ns5li1.fsf@gmx.net> (raw)
In-Reply-To: <6554cc02-2082-3dca-73cc-05fee1c6c4d0@yahoo.de> (R. Diez's message of "Sun, 6 Nov 2022 12:05:07 +0100")

On Sun, 6 Nov 2022 12:05:07 +0100 "R. Diez" <rdiezmail-emacs@yahoo.de> wrote:

> Hi all:
>
> I want to build a list of command-line arguments to run a
> program. Each element of the list must be a string.
[...]
> However, I got into trouble with 'append'. Its documentation states:
>
> "Each argument may be a list, vector or string."
>
> I tested it like this:
>
> (append "1")      -> "1"
> (append "1" "2")  -> (49 . "2")
>
> That behaviour is surprising and it does not fit the bill.

It appears you didn't appreciate the preceding line of the doc string:
"The result is a list whose elements are the elements of all the
arguments."  So the result contains the elements of the string "1",
which is just the single character ?1, whose value is 49.

> I need a routine like this:
>
> (my-append
>   "1"
>   (list "2" "3")
>   "4"
> )
>
> The result should be:
>  ("1" "2" "3" "4")
>
> As a bonus, 'my-append' should check that all elements are strings, or
> list of strings (no nested lists, and no other data types).

If you are willing to make the argument have this kind of form:

'("1" ("2" "3") "4")

then flatten-tree should do the job.  To also check that the elements
are either a string or a list of strings, you could use this:

(defun my-append (l)
  (dolist (e l)
    (cond ((proper-list-p e)
	   (dolist (s e)
	     (unless (stringp s)
	       (user-error "Invalid list"))))
	  (t (unless (stringp e)
	       (user-error "Invalid list")))))
  (flatten-tree l))

Steve Berman



  reply	other threads:[~2022-11-06 12:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6554cc02-2082-3dca-73cc-05fee1c6c4d0.ref@yahoo.de>
2022-11-06 11:05 ` Surprising behaviour of 'append' with strings R. Diez
2022-11-06 12:13   ` Stephen Berman [this message]
2022-11-06 12:35     ` Emanuel Berg
2022-11-06 20:28       ` Michael Heerdegen
2022-11-06 20:57         ` Emanuel Berg
2022-11-06 12:19   ` Emanuel Berg
2022-11-06 12:32   ` Emanuel Berg
2022-11-06 12:37   ` Eli Zaretskii
2022-11-06 12:39   ` Jean Louis
2022-11-06 20:33   ` Michael Heerdegen
2022-11-06 21:45     ` [External] : " Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v8ns5li1.fsf@gmx.net \
    --to=stephen.berman@gmx.net \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rdiezmail-emacs@yahoo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).