• Loopy

    From B. Pym@21:1/5 to Kent M. Pitman on Sun Jun 9 20:59:10 2024
    Kent M. Pitman wrote:

    The ability in loop to do even complex things like:

    (loop for x in '(1 2 3 4 5 6 7)
    when (evenp x)
    collect x into evens
    else
    collect x into odds
    finally
    (return (values evens odds)))
    (2 4 6), (1 3 5 7)

    Gauche Scheme

    (partition odd? (iota 9)
    ===>
    (1 3 5 7)
    (0 2 4 6 8)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sun Jun 9 21:17:05 2024
    On 6/9/2024, B. Pym wrote:

    Kent M. Pitman wrote:

    The ability in loop to do even complex things like:

    (loop for x in '(1 2 3 4 5 6 7)
    when (evenp x)
    collect x into evens
    else
    collect x into odds
    finally
    (return (values evens odds)))
    (2 4 6), (1 3 5 7)

    Gauche Scheme

    (partition odd? (iota 9)
    ===>
    (1 3 5 7)
    (0 2 4 6 8)

    Also, being able to vary the collection is handy. Consider:

    (loop for x in '(a b (c d) e f)
    when (atom x)
    collect x
    else
    append x)
    (A B C D E F)

    Gauche Scheme

    (append-map
    (lambda(x) (if (pair? x) x (list x)))
    '(a b (c d) e f))

    ===>
    (a b c d e f)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Mon Jun 10 06:17:09 2024
    On 6/9/2024, B. Pym wrote:

    The ability in loop to do even complex things like:

    (loop for x in '(1 2 3 4 5 6 7)
    when (evenp x)
    collect x into evens
    else
    collect x into odds
    finally
    (return (values evens odds)))
    (2 4 6), (1 3 5 7)

    Peter Seibel wrote:

    (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))))


    In Gauche Scheme, it's a one-liner.

    (use gauche.sequence)

    (define v #(0 2 -3 99 48 35 86 27 50 18))
    (define count (vector-length v))


    `(,(find-min v) ,(find-max v) ,(/ (fold + 0 v) count))
    ===>
    (-3 99 181/5)


    --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Mon Jun 10 07:56:39 2024
    On 6/10/2024, B. Pym wrote:

    On 6/9/2024, B. Pym wrote:

    The ability in loop to do even complex things like:

    (loop for x in '(1 2 3 4 5 6 7)
    when (evenp x)
    collect x into evens
    else
    collect x into odds
    finally
    (return (values evens odds)))
    (2 4 6), (1 3 5 7)

    Peter Seibel wrote:

    (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))))



    How about good old "do*"?

    (defparameter v #(0 2 -3 99 48 35 86 27 50 18))
    (defparameter len (length v))

    (do* ((i 0 (1+ i))
    (x (aref v i) (aref v i))
    (mn x (min x mn))
    (mx x (max x mx))
    (sum x (+ sum x)))
    ((= (1+ i) len) (list mn mx (/ sum len))))

    ===>
    (-3 99 181/5)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Thu Jun 13 03:13:11 2024
    On 6/10/2024, B. Pym wrote:

    Peter Seibel wrote:

    (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))))


    In Gauche Scheme, it's a one-liner.

    (use gauche.sequence)

    (define v #(0 2 -3 99 48 35 86 27 50 18))
    (define count (vector-length v))


    `(,(find-min v) ,(find-max v) ,(/ (fold + 0 v) count))
    ===>
    (-3 99 181/5)

    Another way.

    (define v #(0 2 -3 99 48 35 86 27 50 18))
    (define count (vector-length v))

    (let ((r (mul-vec-reduce (list + max min) v)))
    (reverse (cons (/ (pop! r) count) r)))

    ===>
    (-3 99 181/5)

    Given:

    (use srfi-43) ;; vector-fold

    (define (mul-vec-reduce konses vec)
    (let ((len (length konses)))
    (vector-fold
    (lambda (i accum x)
    (if (null? accum)
    (make-list len x)
    (map
    (lambda (f a b) (f a b))
    konses
    (make-list len x)
    accum)))
    '()
    vec)))

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