• Re: Emacs Lisp's "mapconcat" in Common Lisp?

    From B. Pym@21:1/5 to Pascal Bourguignon on Fri Aug 30 23:21:11 2024
    XPost: comp.lang.scheme

    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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Fri Aug 30 23:49:49 2024
    XPost: comp.lang.scheme

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Sat Aug 31 06:19:55 2024
    XPost: comp.lang.scheme

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)