• Re: Merging strings

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Wed Jul 16 22:37:32 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    You have a list contain both strings and numbers.
    Generate a new list in which the adjacent strings
    have been concatenated.

    Scheme

    (define (merge-strings List)
    (reverse
    (fold
    (lambda (e accum)
    (if (and (string? e) (pair? accum) (string? (car accum)))
    (cons (string-append (car accum) e) (cdr accum))
    (cons e accum)))
    '()
    List)))

    (merge-strings '("1" "2" 3 4 "5" "6" 7 8 "9"))
    ===>
    ("12" 3 4 "56" 7 8 "9")



    (defun merge-strings (list)
    (loop while list
    if (stringp (car list)) collect
    (apply #'concatenate 'string
    (loop while (stringp (car list))
    collect (pop list)))
    else collect (pop list)))


    Gauche Scheme

    ;; Doesn't cause an error is list is empty.
    (define (car~ xs) (and (pair? xs) (car xs)))


    (define (merge-strings xs . r)
    (while (pair? xs)
    (push! r
    (if (string? (car xs))
    (let f ()
    (if (string? (car~ xs)) (string-append (pop! xs) (f)) ""))
    (pop! xs))))
    (reverse r))
    --
    [T]he problem is that lispniks are as cultish as any other
    devout group and basically fall down frothing at the mouth if
    they see [heterodoxy]. --- Kenny Tilton (05 Dec 2004)
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 17 10:48:21 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    B. Pym wrote:

    You have a list contain both strings and numbers.
    Generate a new list in which the adjacent strings
    have been concatenated.

    Scheme

    (define (merge-strings List)
    (reverse
    (fold
    (lambda (e accum)
    (if (and (string? e) (pair? accum) (string? (car accum)))
    (cons (string-append (car accum) e) (cdr accum))
    (cons e accum)))
    '()
    List)))

    (merge-strings '("1" "2" 3 4 "5" "6" 7 8 "9"))
    ===>
    ("12" 3 4 "56" 7 8 "9")



    (defun merge-strings (list)
    (loop while list
    if (stringp (car list)) collect
    (apply #'concatenate 'string
    (loop while (stringp (car list))
    collect (pop list)))
    else collect (pop list)))


    Gauche Scheme

    ;; Doesn't cause an error is list is empty.
    (define (car~ xs) (and (pair? xs) (car xs)))


    (define (merge-strings xs . r)
    (while (pair? xs)
    (push! r
    (if (string? (car xs))
    (let f ()
    (if (string? (car~ xs)) (string-append (pop! xs) (f)) ""))
    (pop! xs))))
    (reverse r))

    Shorter.

    (define (merge-strings List)
    (fold-right
    (^(e accum)
    (cons
    (if (and (string? e) (string? (car~ accum)))
    (string-append e (pop! accum))
    e)
    accum))
    ()
    List))
    --
    [T]he problem is that lispniks are as cultish as any other
    devout group and basically fall down frothing at the mouth if
    they see [heterodoxy]. --- Kenny Tilton (05 Dec 2004)
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 17 12:08:31 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    B. Pym wrote:

    B. Pym wrote:

    You have a list contain both strings and numbers.
    Generate a new list in which the adjacent strings
    have been concatenated.

    Scheme

    (define (merge-strings List)
    (reverse
    (fold
    (lambda (e accum)
    (if (and (string? e) (pair? accum) (string? (car accum)))
    (cons (string-append (car accum) e) (cdr accum))
    (cons e accum)))
    '()
    List)))

    (merge-strings '("1" "2" 3 4 "5" "6" 7 8 "9"))
    ===>
    ("12" 3 4 "56" 7 8 "9")



    (defun merge-strings (list)
    (loop while list
    if (stringp (car list)) collect
    (apply #'concatenate 'string
    (loop while (stringp (car list))
    collect (pop list)))
    else collect (pop list)))


    Gauche Scheme

    ;; Doesn't cause an error is list is empty.
    (define (car~ xs) (and (pair? xs) (car xs)))


    (define (merge-strings xs . r)
    (while (pair? xs)
    (push! r
    (if (string? (car xs))
    (let f ()
    (if (string? (car~ xs)) (string-append (pop! xs) (f)) ""))
    (pop! xs))))
    (reverse r))

    Shorter.

    (define (merge-strings List)
    (fold-right
    (^(e accum)
    (cons
    (if (and (string? e) (string? (car~ accum)))
    (string-append e (pop! accum))
    e)
    accum))
    ()
    List))

    Shorter.

    (define (merge-strings List)
    (fold-right*
    (^(e accum both)
    (cons
    (if (every string? both)
    (string-append e (pop! accum))
    e)
    accum))
    ()
    List))

    Given:

    (define (fold-right* kons knil xs)
    (fold-right
    (lambda (e accum)
    (if (pair? accum)
    (kons e accum (list e (car accum)))
    (kons e accum (list e #f)))
    )
    knil
    xs))
    --
    [T]he problem is that lispniks are as cultish as any other
    devout group and basically fall down frothing at the mouth if
    they see [heterodoxy]. --- Kenny Tilton (05 Dec 2004)
    --- Synchronet 3.21a-Linux NewsLink 1.2