• Re: Multivalue tail recursion?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Thu Jun 19 21:10:54 2025
    From Newsgroup: comp.lang.lisp


    (loop for x in '(3 5 7)
    for y in '(2 5 8)
    when (= x y)
    collect (cons x y))


    What if there were more than two lists?
    This will handle any number of lists:

    Scheme:

    (filter-map
    (lambda xs (and (apply = xs) xs))
    '(0 3 5 7 9)
    '(0 2 5.0 8 9.0)
    '(2 4 5 8 9))

    ((5 5.0 5) (9 9.0 9))
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.lisp on Thu Jun 19 22:40:27 2025
    From Newsgroup: comp.lang.lisp

    On 2025-06-19, B. Pym <Nobody447095@here-nor-there.org> wrote:

    (loop for x in '(3 5 7)
    for y in '(2 5 8)
    when (= x y)
    collect (cons x y))


    What if there were more than two lists?
    This will handle any number of lists:

    Scheme:

    (filter-map
    (lambda xs (and (apply = xs) xs))

    What's wrong with (if (apply = xs) xs)?

    '(0 3 5 7 9)
    '(0 2 5.0 8 9.0)
    '(2 4 5 8 9))

    ((5 5.0 5) (9 9.0 9))

    This is nicer:

    (mappend [iff = [chain list list]]
    '(0 3 5 7 9)
    '(0 2 5.0 8 9.0)
    '(2 4 5 8 9))
    ((5 5.0 5) (9 9.0 9))
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Alan Bawden@alan@csail.mit.edu to comp.lang.lisp on Thu Jun 19 19:22:49 2025
    From Newsgroup: comp.lang.lisp

    Kaz Kylheku <643-408-1753@kylheku.com> writes:

    > Scheme:
    >
    > (filter-map
    > (lambda xs (and (apply = xs) xs))

    What's wrong with (if (apply = xs) xs)?

    In Scheme, (if #F whatever) is undefined, while (and #F whatever) is #F.

    At least that's the way it was in R5RS. I don't have a more recent
    version of the specification handy.

    (Meanwhile in Common Lisp, (if nil whatever) is nil.)

    (Kaz: Why are you wasting your time replying to this junk?)
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Lawrence D'Oliveiro@ldo@nz.invalid to comp.lang.lisp on Fri Jun 20 00:52:46 2025
    From Newsgroup: comp.lang.lisp

    On Thu, 19 Jun 2025 19:22:49 -0400, Alan Bawden wrote:

    At least that's the way it was in R5RS. I don't have a more recent
    version of the specification handy.

    <http://www.r6rs.org/>
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 3 16:42:37 2025
    From Newsgroup: comp.lang.lisp

    Ken Tilton wrote:

    Anyway, I whipped one up in a few lines that not only pulls n items
    off the list, but it returns the remaining items as well. Let me know >>what you think. I was kind of surprised that I was able to do it
    without the use of an accumulator parameter.

    (defun first-n (n list)
    "Splits the list after the first n elements"
    (if (= 0 n)
    (values '() list)
    (multiple-value-bind (from-front rest) (first-n (1- n) (rest
    list))
    (values (cons (first list) from-front) rest))))


    I would probably do something like this instead:

    (defun first-n (n list)
    (loop for i below n for (a . d) on list
    collect a into x
    finally (return (values x d))))

    Niiiiiiice.

    Gauche Scheme:

    (define (first-n n xs . ys)
    (dotimes (n) (push! ys (pop! xs)))
    (values (reverse ys) xs))
    --- Synchronet 3.21d-Linux NewsLink 1.2