Re: Another code review perhaps?

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Another code review perhaps?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 24. Jun 2025, 14:31:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103e9bp$21nrq$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
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)


Date Sujet#  Auteur
12 Sep 24 * Re: Another code review perhaps?3B. Pym
12 Sep 24 +- Re: Another code review perhaps?1Kaz Kylheku
24 Jun 25 `- Re: Another code review perhaps?1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal