help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Surprising behaviour of 'append' with strings


From: Stephen Berman
Subject: Re: Surprising behaviour of 'append' with strings
Date: Sun, 06 Nov 2022 13:13:26 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]