On Monday, February 11, 2019 at 8:01:47 AM UTC+8, travis.h...@gmail.com 
wrote:
>
> 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.
>

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)


Alex. 


> (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)
>   (if (zero? iter) n
>       (pop-projection A (matrix* A n) (- iter 1))))
>
> (pop-projection A n 25)
>
>
>
> On Sunday, February 10, 2019 at 3:56:25 AM UTC-8, Daniel Prager wrote:
>>
>> Thanks for the screenshot Travis.
>>
>> Just for fun, here's a version in Racket that eschews assignment, vectors 
>> and for loops, in favour of recursion and lists ...
>>
>> #lang racket
>>
>> (define years 30)
>> (define prop-female 0.5)
>> (define egg-surv 0.6)
>>
>> (define fecundity '(0 0 200 400 800))
>> (define survival '(0.2 0.4 0.6 0.8 0))
>> (define capacity '(1e6 1e5 1e4 1e3 1e2 -9999))
>> (define cap0 (first capacity))
>>
>> (define (beverton-holt N p c) (/ N (+ (/ 1 p) (/ N c))))
>>
>> (define (evolve N [f fecundity] [s survival] [cap (rest capacity)] [Nt0 
>> 0] [Nt null])
>>     (if (null? f)
>>         (cons Nt0 (reverse Nt))
>>         (evolve (rest N) (rest f) (rest s) (rest cap)
>>                 (+ Nt0 (if (= (first f) 0)
>>                            0
>>                            (beverton-holt (* (first N) prop-female)
>>                                           (* (first f) egg-surv)
>>                                           (- cap0 Nt0))))
>>                 (if (= (first s) 0)
>>                     Nt
>>                     (cons (beverton-holt (first N) (first s) (first cap)) 
>> Nt)))))
>>
>> (define (iterate N n [i 1])
>>   (displayln (list i N))
>>   (unless (= i n) (iterate (evolve N) n (+ i 1))))
>>
>> (iterate (make-list (length fecundity) 10) years)
>>
>

-- 
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