> On Feb 10, 2019, at 7:26 PM, Alex Harsanyi <alexharsa...@gmail.com> wrote:
> 
> One way to do this is for `pop-abundances` to have an extra parameter, the 
> list of previous abundances, and whenever the function is called recursively, 
> it adds the current abundance to this list and passes it on to the next call. 
>  The final call will than return this result instead of the last abundance.  
> In the example below, "cons" adds to the front of the list, so "result" 
> contains the most recent values first and  this list is reversed before being 
> returned to the user.  Also, when `pop-abundances` is invoked by the user, 
> there are no "previous abundances" , so it needs to be invoked with an empty 
> list -- this is handled by a default parameter for 'result':
> 
> #lang racket
> (require math/matrix)
> 
> (define A (matrix [[0 0 5.905]
>                    [0.368 0.639 0.025]
>                    [0.001 0.152 0.051]]))
> 
> (define n (col-matrix [5 5 5]))
> 
> (define (pop-projection A n iter [result '()])
>   (if (zero? iter)
>       (reverse (cons n result))
>       (pop-projection A (matrix* A n) (- iter 1) (cons n result))))
> 
> (pop-projection A n 25)
> 



Don’t use accumulators if the function has all the information: 

#lang racket

(require math/matrix)

(define A
  (matrix
   [[0     0     5.905]
    [0.368 0.639 0.025]
    [0.001 0.152 0.051]]))

(define n (col-matrix [5 5 5]))

(define (pop-projection A n iter [result '()])
  (if (zero? iter)
      (reverse (cons n result))
      (pop-projection A (matrix* A n) (- iter 1) (cons n result))))

(define (pop-projection.v1 A n iter)
  (if (zero? iter)
      (list n)
      (cons n (pop-projection A (matrix* A n) (- iter 1)))))

(equal? (pop-projection A n 25) (pop-projection.v1 A n 25))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to