• Re: Another code review perhaps?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Wed Jul 9 21:18:40 2025
    From Newsgroup: comp.lang.scheme

    Peter Seibel wrote:

    This is my solution to Ex. 5 on p. 97 of Paul Graham's "ANSI Common
    Lisp"

    <QUOTE>
    Define iterative and recursive versions of a function that takes an
    object x and a vector v, and returns a list of all the objects that immediately precede x in v.

    (precedes #\a "abracadabra")
    (#\c #\d #\r)
    </QUOTE>


    (defun precedes (object vector)
    (do ((length (length vector))
    (results nil)
    (idx 1 (1+ idx)))
    ((= idx length) results)
    (when (eql object (aref vector idx))
    (pushnew (aref vector (1- idx)) results))))

    I don't think that's really any better. Maybe LOOP:

    (defun precedes (object vector)
    (loop with results = nil
    for idx from 1 below (length vector)
    when (eql object (aref vector idx))
    do (pushnew (aref vector (1- idx)) results)
    finally (return results)))

    Gauche Scheme

    (use gauche.sequence)

    (define (precedes obj seq)
    (do_ ((i 1 :below (size-of seq))
    (r '()))
    (#f @ r)
    (when (eqv? obj (ref seq i))
    (let1 prev (ref seq (- i 1))
    (or (member prev r) (push! r prev))))))


    (precedes #\a "abracadabra")

    ===>
    (#\r #\c #\d)

    Given:

    (define-syntax do_-aux
    (syntax-rules ( <> @ :in :collect-if :collect :below :to : )
    [ (do_-aux ((x what <>) more ...) (seen ...) stuff ...)
    (do_-aux (more ...) (seen ... (x what what)) stuff ...) ]
    [ (do_-aux ((x a :below b) more ...) seen lets (bool z ...) stuff ...)
    (do_-aux ((top b)
    (x a (+ x 1)) more ...) seen lets
    ((or (>= x top) bool) z ...) stuff ...) ]
    [ (do_-aux ((x a :to b) more ...) stuff ...)
    (do_-aux ((x a :below (+ 1 b)) more ...) stuff ...) ]
    [ (do_-aux ((x :in seq) more ...) seen (lets ...) (bool z ...) stuff ...)
    (do_-aux ((x (and (pair? the-list) (car the-list)) <>) more ...)
    seen
    (lets ... (the-list seq))
    ((or (null? the-list) (begin (pop! the-list) #f) bool) z ...)
    stuff ...) ]
    [ (do_-aux ((accum :collect-if bool x) more ...) stuff ...)
    (do_-aux ((accum '()
    (if bool (cons x accum) accum)) more ...) stuff ...) ]
    [ (do_-aux ((accum :collect x) more ...) stuff ...)
    (do_-aux ((accum :collect-if #t x) more ...) stuff ...) ]
    [ (do_-aux (: v init update more ...) (seen ...) stuff ...)
    (do_-aux (: more ...) (seen ... (v init update)) stuff ...) ]
    [ (do_-aux (:) stuff ...)
    (do_-aux () stuff ...) ]
    [ (do_-aux (spec more ...) (seen ...) stuff ...)
    (do_-aux (more ...) (seen ... spec) stuff ...) ]
    [ (do_-aux () seen lets (bool y ... @ result) stuff ...)
    (do_-aux () seen lets (bool y ... (reverse result)) stuff ...) ]
    [ (do_-aux () seen (lets ...) more ...)
    (let (lets ...)
    (do seen more ...))
    ] ))
    (define-syntax do_
    (syntax-rules ()
    [ (do_ specs () more ...)
    (do_ specs (#f) more ...) ]
    [ (do_ specs more ...)
    (do_-aux specs () () more ...) ] ))
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Sep 12 09:12:38 2024
    From Newsgroup: comp.lang.scheme

    Arthur Lemmens wrote:

    Define iterative and recursive versions of a function that takes an
    object x and a vector v, and returns a list of all the objects that immediately precede x in v.

    Graham doesn't like LOOP, but I do. So here's a LOOP-version:

    (defun precedes (object vector)
    (loop for x across vector
    and i from 0
    when (and (equal x object) (> i 0))
    collect (elt vector (1-i))))


    Look at that:

    (1-i)

    Don't you think that that should be:

    (1- i)

    or

    (- i 1)

    ?

    It's shorter when you use a Lispy language instead of CL.

    Gauche Scheme

    (use srfi-42) ; list-ec

    (define (precedes obj vec)
    (list-ec (: x (index i) vec)
    (if (and (> i 0) (equal? x obj)))
    (ref vec (- i 1))))

    (precedes 5 #(5 0 4 5 8 9 5))
    ===>
    (4 9)

    Another way:

    (define (precedes o v)
    (let ((l (vector->list vec)))
    (filter-map
    (^(a b) (and (equal? o a) b))
    (cdr l)
    l)))
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.lisp,comp.lang.scheme on Thu Sep 12 12:22:24 2024
    From Newsgroup: comp.lang.scheme

    On 2024-09-12, B. Pym <Nobody447095@here-nor-there.org> wrote:
    Arthur Lemmens wrote:

    Define iterative and recursive versions of a function that takes an
    object x and a vector v, and returns a list of all the objects that
    immediately precede x in v.

    Graham doesn't like LOOP, but I do. So here's a LOOP-version:

    (defun precedes (object vector)
    (loop for x across vector
    and i from 0
    when (and (equal x object) (> i 0))
    collect (elt vector (1-i))))


    Look at that:

    (1-i)

    Don't you think that that should be:

    (1- i)

    or

    (- i 1)

    ?

    It's shorter when you use a Lispy language instead of CL.

    Gauche Scheme

    (use srfi-42) ; list-ec

    (define (precedes obj vec)
    (list-ec (: x (index i) vec)
    (if (and (> i 0) (equal? x obj)))
    (ref vec (- i 1))))

    (precedes 5 #(5 0 4 5 8 9 5))

    (4 9)

    Another way:

    (define (precedes o v)
    (let ((l (vector->list vec)))
    (filter-map
    (^(a b) (and (equal? o a) b))
    (cdr l)
    l)))

    (match @(scan-all (@x 5 . @nil)) '(5 0 4 5 8 9 5) x)
    (4 9)
    (window-mappend 1 nil (do if (and @1 (= @2 5)) (list @1)) #(5 0 4 5 8 9 5))
    #(4 9)
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Tue Jun 24 13:31:40 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    Arthur Lemmens wrote:

    Define iterative and recursive versions of a function that takes an object x and a vector v, and returns a list of all the objects that immediately precede x in v.

    Graham doesn't like LOOP, but I do. So here's a LOOP-version:

    (defun precedes (object vector)
    (loop for x across vector
    and i from 0
    when (and (equal x object) (> i 0))
    collect (elt vector (1-i))))


    Look at that:

    (1-i)

    Don't you think that that should be:

    (1- i)

    or

    (- i 1)

    ?

    It's shorter when you use a Lispy language instead of CL.

    Gauche Scheme

    (use srfi-42) ; list-ec

    (define (precedes obj vec)
    (list-ec (: x (index i) vec)
    (if (and (> i 0) (equal? x obj)))
    (ref vec (- i 1))))

    (precedes 5 #(5 0 4 5 8 9 5))
    ===>
    (4 9)

    Another way:

    (define (precedes o v)
    (let ((l (vector->list vec)))
    (filter-map
    (^(a b) (and (equal? o a) b))
    (cdr l)
    l)))

    Shorter:

    (use srfi-42) ;; list-ec
    (use gauche.sequence) ;; subseq

    (define (precedes obj vec)
    (list-ec (:parallel (: b (subseq vec 1)) (: a vec))
    (if (equal? b obj))
    a))

    (precedes '! #(! 0 ! 3 4 !))
    ===>
    (0 4)

    --- Synchronet 3.22a-Linux NewsLink 1.2