For the benefit of other beginners, I think there was a small typo in 
prop-projection.v1 where the intention was to call that function 
recursively rather than calling pop-projection in the body of 
pop-projection.v1. The typo is corrected below:

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

Thanks to everyone for the feedback. It has been very illuminating!



On Sunday, February 10, 2019 at 5:49:01 PM UTC-8, Matthias Felleisen wrote:
>
>
>
> On Feb 10, 2019, at 7:26 PM, Alex Harsanyi <alexha...@gmail.com 
> <javascript:>> 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