Cumulative standard deviation: Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) (→{{header|R}}: Solution that gives every result so far, rather than just the final result.) |
ReeceGoding (talk | contribs) (→{{header|R}}: Added what I believe to be a true stateful solution. I'm not well-informed about the terminology. Hopefully I've not accidentally made a generator.) |
||
Line 3,302: | Line 3,302: | ||
<pre>> cumSd(c(2, 4, 4, 4, 5, 5, 7, 9)) |
<pre>> cumSd(c(2, 4, 4, 4, 5, 5, 7, 9)) |
||
[1] 0.0000000 1.0000000 0.9428090 0.8660254 0.9797959 1.0000000 1.3997084 2.0000000</pre> |
[1] 0.0000000 1.0000000 0.9428090 0.8660254 0.9797959 1.0000000 1.3997084 2.0000000</pre> |
||
===Stateful SD=== |
|||
If we want a function that remembers and uses the previous inputs, letting us be very strict about the "one at a time" requirement, then we can lift biasedSd from the previous solution and make good use of the distinction between R's <- and <<- methods of assignment. |
|||
<lang r>cumSDStateful<-function() |
|||
{ |
|||
data<-numeric(0) |
|||
function(oneNumber) |
|||
{ |
|||
data<<-c(data,oneNumber) |
|||
biasedSd(data) |
|||
} |
|||
} |
|||
state<-cumSDStateful()</lang> |
|||
{{out}} |
|||
<pre>> state(2);state(4);state(4);state(4);state(5);state(5);state(7);state(9) |
|||
[1] 0 |
|||
[1] 1 |
|||
[1] 0.942809 |
|||
[1] 0.8660254 |
|||
[1] 0.9797959 |
|||
[1] 1 |
|||
[1] 1.399708 |
|||
[1] 2</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |