Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You will never know hunger.


comp / comp.lang.lisp / Re: Emacs Lisp's "mapconcat" in Common Lisp?

SubjectAuthor
* Re: Emacs Lisp's "mapconcat" in Common Lisp?B. Pym
`* Re: Emacs Lisp's "mapconcat" in Common Lisp?B. Pym
 `- Re: Emacs Lisp's "mapconcat" in Common Lisp?Kaz Kylheku

1
Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 23:21 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
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=
View all headers

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"

Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 23:49 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
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=
View all headers

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)))

Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 31 Aug 2024 06:19 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
Date: Sat, 31 Aug 2024 06:19:55 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20240830231523.789@kylheku.com>
References: <vatk56$md9s$1@dont-email.me> <vatlqs$mk6q$1@dont-email.me>
Injection-Date: Sat, 31 Aug 2024 08:19:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f97a594c41957bc9e61a815e61b46e34";
logging-data="969225"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lKJrOJSx33wqVQxYEytJ2GOZvX4E64AI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:exMxh2GJIh8xqFs0Schkw4WUINc=
View all headers

On 2024-08-30, B. Pym <Nobody447095@here-nor-there.org> wrote:
> Shorter:
>
> (define (mapconcat fun lst sep)
> (string-join (map (compose x->string fun) lst) (x->string sep)))

"Concatenate" is a pleonasm for "catenate", which is based on a
Latin root for "chain". Chaining is implicitly together ("con").
Even the Unix dolts understood this, so we have /bin/cat
and strcat, and not /bin/concat and strconcat.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1

rocksolid light 0.9.8
clearnet tor