list - F# tricky recursive algorithm -
i have code in vba (looping through array a() of type double):
bm = 0 'tot b = 0 'prev = 24 0 step -1 bp = b 'prevprev = prev b = bm 'prev = tot bm = t * b - bp + a(i) 'tot = a(i) + t * prev - prevprev next p = exp(-xa * xa) * (bm - bp) / 4 '* (tot - prevprev)/4
i'm putting in f#. use array , mutable variables recreate vba. , maybe example of right time use mutable i've seen hinted at. why not try idiomatic way?
i write little recursive function replicate loop. kind of feels littering hang out little sub-loop has no meaning on own standalone, named function.
i want list functions. have couple ideas, i'm not there yet. in snap??
the 2 vague ideas have are: 1. make 2 more lists chopping off 1 (and two) elements , adding zero-value element(s). , combine lists. 2. i'm wondering if list function map can take trailing terms in list arguments. 3. general question, wonder if might case experienced person problem screams mutable values (and if dampen enthusiasm getting on functional boat).
to give more intuition code: full function excerpted numerical approximation cumulative normal distribution. haven't looked math behind one. "xa" absolute value of main function argument "x" number of standard deviations zero. without working through proof, don't think there's more than: it's formula. (oh , maybe should change variable names--xa , bm etc pretty wretched. did put suggestions comments.)
it's standard recursion. make exit condition , recur condition.
let rec calc prevprev prev total = if = 0 // exit condition; final calc exp(-xa * xa) * (total - prevprev) / 4. else // recur condition, call again let newprevprev = prev let newprev = total let newtotal = (t * newprev - newprevprev + i) calc (i-1) newprevprev newprev newtotal calc 24 initprevprev initprev inittotal
or shorter...
let rec calc prevprev prev total = if = 0 exp(-xa * xa) * (total - prevprev) / 4. else calc (i-1) prev total (t * total - prev + i)
Comments
Post a Comment