• Re: The LOOP macro

    From B. Pym@21:1/5 to All on Mon Aug 5 04:53:00 2024
    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)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Mon Aug 5 18:18:22 2024
    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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to All on Sat Aug 31 20:02:56 2024
    XPost: comp.lang.scheme

    Or if the predicate functions FOO-P and BAR-P are sufficiently
    expensive that you don't want to compute more often than absolutely necessary:

    (loop for x in things
    for foo-p = (foo-p x)
    for bar-p = (bar-p x)
    when foo-p collect x into foos
    when bar-p collect x into bars
    when (and foo-p bar-p) collect x into both
    finally (return (values foos bars both)))

    Gauche Scheme

    (define things '(2 9 33 -44 0 5 -27 88 6 99 -7))

    (rlet1 al '()
    (dolist (x things)
    (let ((odd-p (odd? x)) (neg-p (negative? x)))
    (if odd-p (apush! al 'odd x))
    (if neg-p (apush! al 'neg x))
    (and odd-p neg-p (apush! al 'both x)))))

    ((both -7 -27) (neg -7 -27 -44) (odd -7 99 -27 5 33 9))

    Given:

    (define-syntax ainc!
    (syntax-rules ()
    [(_ alist key val func default)
    (let ((pair (assoc key alist)))
    (if pair
    (set-cdr! pair (func val (cdr pair)))
    (set! alist (cons (cons key (func val default)) alist))))]
    [(_ alist key val func)
    (ainc! alist key val func 0)]
    [(_ alist key val)
    (ainc! alist key val +)]
    [(_ alist key)
    (ainc! alist key 1)]))

    (define-syntax apush!
    (syntax-rules ()
    [(_ alist key val) (ainc! alist key val cons '())]))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Kenny Tilton on Thu Sep 12 06:36:45 2024
    Kenny Tilton wrote:

    (loop for n below 3 do (print n))

    That ain't Lisp syntax. This is Lisp syntax;

    (do ((n 1 (1+ n)))
    ((> n 3))
    (print n))

    (lope ! print upto 0 2)

    ===>
    0
    1
    2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)