| Deutsch English Français Italiano |
|
<vatlqs$mk6q$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:49:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <vatlqs$mk6q$1@dont-email.me>
References: <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:49:50 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f26fadc2861a453a8f872ea6dc048189";
logging-data="741594"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qMnUmbORdmVyLRsFXMNRs"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:pqBUrB3V6J+Z06+p6FS8kzl6nac=
Bytes: 2709
B. Pym wrote:
> 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"
Shorter:
(define (mapconcat fun lst sep)
(string-join (map (compose x->string fun) lst) (x->string sep)))