Re: The LOOP macro

Liste des GroupesRevenir à cl lisp 
Sujet : Re: The LOOP macro
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 05. Aug 2024, 19:18:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8r51a$v47e$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Do you have a good example of LOOP's power / flexibility that doesn't
need much surrounding context to understand?
 
Here are a few:
 
  (loop repeat 100 collect (random 10))
 
 
newLISP
 
(collect (rand 10) 100)
 
 
 
  (loop for x across array-of-numbers
        minimizing x into min
        maximizing x into max
        summing x into total
        counting t into count
        finally (return (list min max (/ total count))))
 
 
(define array-of-numbers (array 9 '(2 -2 0 9 7 8 3 4 3)))
 
(local (mx mn total cnt)
  (dolist (n array-of-numbers)
    (setq mx (max (or mx n) n))
    (setq mn (min (or mn n) n))
    (++ total n)
    (++ cnt))
  (list mn mx (div total cnt)))
 
(-2 9 3.777777777777778)
 
Another way:
 
(list (apply min array-of-numbers)
      (apply max array-of-numbers)
      (div (apply + array-of-numbers) (length array-of-numbers)))


Another way:

(list (apply min array-of-numbers)
      (apply max array-of-numbers)
      ((stats array-of-numbers) 1))

(-2 9 3.777777777777778)

 


Date Sujet#  Auteur
5 Aug 24 * Re: The LOOP macro2B. Pym
5 Aug 24 `- Re: The LOOP macro1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal