--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Performance Warning: Indexing the elements of arrays created in untyped Racket is currently 25-50 times slower than doing the same in Typed Racket, due to the overhead of checking higher-order contracts. We are working on it.
Performance Warning: Indexing the elements of arrays created in untyped Racket is currently 25-50 times slower than doing the same in Typed Racket, due to the overhead of checking higher-order contracts. We are working on it.
--
Thanks, Daniel, this is helpful. I think that I understand your code, but it is a still a foreign way of thinking for me. Of course, that is a big part of why I'm learning Racket, i.e., to make programming with lists and recursion more natural. One key gap for me is how to build up data structures using lists and recursion. Perhaps the answer is that you don't build up the data structure in memory but by writing to disk. In this particular example, it is useful to have the values at every iteration for plotting the population trajectory (by age class or summed across age classes).Below is a little example I was exploring that involves matrix multiplication (col-matrix is abundance in different age classes; square matrix is transition probabilities among age classes). It was obvious to me how to use recursion to get the final col-matrix of abundances but not how to build up a data structure that included the abundances at every iteration.
On Feb 10, 2019, at 7:26 PM, Alex Harsanyi <alexha...@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)