• Re: simple loop question

    From B. Pym@21:1/5 to Lars Brinkhoff on Sun Sep 22 06:45:50 2024
    XPost: comp.lang.scheme

    Lars Brinkhoff wrote:

    use LOOP to collect random integers into a list until the sum of that
    list exceeds a constant (say 50).

    (loop for x = (random 10) collect x sum x into y until (> y 50))

    Let's see if it's shorter in Gauche Scheme.

    (loop (y) (:coll (++. y (random 10))) (:till (> y 50)))

    Yes, it is.

    Given:

    (use srfi-27) ;; random-integer
    (define random random-integer)

    ;; Returns the added number, not the sum.
    (define-syntax ++.
    (syntax-rules ()
    [(++. place val)
    (let ((x val)) (set! place (+ x place)) x)]
    [(++. place) (++. place 1)]))

    I don't want to include the rather lengthy source for
    "loop" here, so defining it is left as an exercise for
    the reader.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Lars Brinkhoff on Tue Sep 3 07:29:30 2024
    XPost: comp.lang.scheme

    Lars Brinkhoff wrote:

    use LOOP to collect random integers into a list until the sum of that
    list exceeds a constant (say 50).

    (loop for x = (random 10) collect x sum x into y until (> y 50))

    Gauche Scheme

    (let1 y 0 (collect-till x (random 10) (> (inc! y x) 50)))
    ===>
    (7 9 4 0 6 4 1 3 2 7 0 7 0 1)


    Given:

    (use srfi-27) ;; random-integer

    (define random random-integer)

    (define-syntax collect-till
    (syntax-rules ()
    [(collect-till v expr0 expr1)
    (let ((res '()))
    (let go ((v expr0))
    (set! res (cons v res))
    (if expr1 (reverse res) (go expr0))))]))

    (collect-till x (random 10) (even? x))
    ===>
    (7 1 6)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Wed Sep 4 00:47:21 2024
    XPost: comp.lang.scheme

    B. Pym wrote:

    Lars Brinkhoff wrote:

    use LOOP to collect random integers into a list until the sum of that list exceeds a constant (say 50).

    (loop for x = (random 10) collect x sum x into y until (> y 50))

    Gauche Scheme

    (let1 y 0 (collect-till x (random 10) (> (inc! y x) 50)))
    ===>
    (7 9 4 0 6 4 1 3 2 7 0 7 0 1)


    Given:

    (use srfi-27) ;; random-integer

    (define random random-integer)

    (define-syntax collect-till
    (syntax-rules ()
    [(collect-till v expr0 expr1)
    (let ((res '()))
    (let go ((v expr0))
    (set! res (cons v res))
    (if expr1 (reverse res) (go expr0))))]))

    (collect-till x (random 10) (even? x))
    ===>
    (7 1 6)


    Revision:

    (collect-till (x (y 0)) (random 10) x (> (inc! y x) 50))

    Given:

    (define-syntax collect-till
    (syntax-rules ()
    [(collect-till (v (w x) ...) v-val collect test)
    (let ((res '()) (w x) ...)
    (let go ((v v-val))
    (set! res (cons collect res))
    (if test (reverse res) (go v-val))))]
    [(collect-till v v-val collect test)
    (collect-till (v) v-val collect test)]))

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