Deutsch   English   Français   Italiano  
<vatk56$md9s$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
Date: Fri, 30 Aug 2024 23:21:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <vatk56$md9s$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 31 Aug 2024 01:21:12 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c51450164ff44e4b4c9e62a2e4bb2981";
	logging-data="734524"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19TqwSNpVKoFsW4j+yAiw7e"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:+B1JSvkRstKdjfSkFDQdi1Jr8As=
Bytes: 2412

Pascal Bourguignon wrote:

> Teemu Likonen <tlikonen@iki.fi> writes:
> 
> > It's a Common Lisp newbie here; I'm more experienced in Emacs Lisp. I
> > wonder if there is similar function in CL like Emacs Lisp's "mapconcat":
> >
> >     (mapconcat 'identity '("one" "two" "three") "-")
> >     => "one-two-three"
> >
> > I can do the same in CL with this:
> >
> >     (let ((list '("one" "two" "three")))
> >       (format nil "~{~a-~}~a" (butlast list) (car (last list))))
> >
> > But I have a feeling that there could be a more elegant way. Is there?
> 
> Yes, write:       (mapconcat 'identity '("one" "two" "three") "-") ; elegant!
> 
> 
> Of course, with:
> 
> 
> (defun mapconcat (fun list sep)
>    (when list
>       (let ((~sep (with-output-to-string (*standard-output*)
>                      (map nil (lambda (ch) (princ (if (char= #\~ ch) "~~" ch)))
> sep))))
>        (format nil (format nil "~~A~~{~A~~A~~}" ~sep)
>                (funcall fun (first list))
>                (mapcar fun (rest list))))))
> 
> 
> 
> (mapconcat 'identity '("one" "two" "three") "-")
> --> "one-two-three"
> 
> (mapconcat (lambda (x) (concatenate 'string "[" x "]")) '("one" "two" "three") "
> ~")
> --> "[one]~[two]~[three]"

Gauche Scheme:

(define (mapconcat fun lst sep)
  (reduce-right
    (^(a b)
      (apply string-append (map x->string (list a sep b))))
    ""
    (map fun lst)))

(mapconcat values '(one "two" three) '-)
  ===>
"one-two-three"

(mapconcat square '(2 3 4) '---)
  ===>
"4---9---16"