[ prog / sol / mona ]

prog


What are you working on?

19 2018-12-28 20:08

>>18

There is an extract-5 in radix-delete for some reason.

I corrected this shortly after I posted that version, I guess I replaced those manually? very odd.

Reusing the name list->radix for the helper in (radix . elements) seems a bit inelegant to me.

I actually should've moved that into list->radix awhile ago, apply also has a bit of overhead that this avoids.

However, my suggestion is to construct the modulo piece first, then write a simple loop from below the modulo downward to zero and append the partial result to the new piece. Since the radix is accessed with radix-ref-height the same amount of accessing work is done by iterating backward. However, this way you would be appending to a piece of constant length at each step, which is linear behavior overall, whereas in your current version each step appends to a piece of linearly increasing length, which is quadratic behavior overall. Additionally, there is nothing forcing you to stick with vector->list and append!. You can have a helper deal with each new piece by consing a series of vector-refs backwards onto the result list, eliminating the need for vector->list and ditching append! and its import entirely.

This is a awesome suggestion!

(define (radix->list radix-struct)
  (define length (radix-length radix-struct))

  (define (vector->partial-list vector list)
    (let vector->partial-list-iter ((list list) (index 31))
      (if (= index 0)
        (cons (vector-ref vector index) list)
        (vector->partial-list-iter
          (cons (vector-ref vector index) list)
          (- index 1)))))

  (let radix->list-iter
    ((list
      (vector->list
        (vector-copy
          (radix-ref-height radix-struct length 1)
          0
          (modulo length 32))))
     (position
       (- length 32 (modulo length 32))))
    (if (< position 0)
      list
      (radix->list-iter
        (vector->partial-list
          (radix-ref-height radix-struct position 1)
          list)
        (- position 32)))))

From your ease of use of named lets and the continuation trick of naive-append-vector!, I doubt that.

Thanks anon.

199


VIP:

do not edit these