[racket] Mutable state vs RAM on fire
You're right; your version is easier on the eyes.
Starting from 1 instead of 0 was deliberate, however.
DZ's method involves multiplying N polynomials of degree X, where X is the number of sides on a die and N is the number of dice.
So, 2d6 would be like this:
(x^1 + x^2 + x^3 + x^4 + x^5 + x^6) (x^1 + x^2 + x^3 + x^4 + x^5 + x^6)
With the final exponents as the number rolled, and the final coefficients as the chance (out of 6^2) to roll it.
Which is why I used alists, at first; I don't really need the 0 part of the vector.
I think I will give hashes a try.
tk
On Wed, May 02, 2012 at 05:08:03PM -0400, Matthias Felleisen wrote:
>
> > I rewrote the thing to use vectors instead, and altered the polynomial multiplication function to use (begin) and (vector-set!):
> >
> > https://github.com/TurtleKitty/Dice/blob/67c2b49707132395f73b43afe111e3904b3898f2/dice.rkt
> >
> > It too now calculates three hundred dice without breaking a sweat, but... I feel dirty.
>
>
> It's also wrong and stylistically bad:
>
> (define (poly-mul p1 p2)
> (define deg1 (poly-deg p1))
> (define deg2 (poly-deg p2))
> (define noob (make-vector (- (+ deg1 deg2) 1)))
> ;; MF: bug, these were 1s:
> (for* ([i (in-range 0 deg1)] [j (in-range 0 deg2)])
> (define k (+ i j))
> (define a (* (vector-ref p1 i) (vector-ref p2 j)))
> (vector-set! noob k (+ (vector-ref noob k) a)))
> noob)
>
>
>
> > Can anyone recommend a functional approach that won't melt my motherboard?
> > I'm considering hashes, since they have the immutable version of hash-set that vectors seem to lack, but I thought I'd ask the experts.
>
>
> Do try hash and for/hash. I think you will be pleased with the performance. -- Matthias
>
> p.s. Do report back.
>
>