This program demonstrates some statistical calculations.

# The program is:

## Statistical functions

\list_mean=(\xs / (list_sum xs) (list_length xs))

\list_variance=
(\list
\mean=(list_mean list)
\diff=(map (\val - val mean) list)
\square_diff=(map (\val * val val) diff)
list_mean square_diff
)

\list_stddev=(\list sqrt; list_variance list)

## Output functions

\p=(say "<p>")
\pre=(tag "pre")

\show=
(\label\val
put [label " = "]
is_list val
(each val \x put [x " "])
(put val)
nl
)

## (show_stats list) Shows the statistics for the given list.
\show_stats=
(\list
tag "h2" (say "Show Stats")
p
say "First call the list_stddev function directly to get the final answer."

pre
(
show "list" list
\stddev=(list_stddev list)
show "stddev" stddev
)

p
say "Now show how we arrive at that answer."

pre
(
\sum=(list_sum list)
\count=(list_length list)
\mean=(list_mean list)

show "list" list
show "sum" sum
show "count" count
show "mean" mean

\diff=(map (\val - val mean) list)
\square_diff=(map (\val * val val) diff)

show "diff" diff
show "square_diff" square_diff

\variance=(list_mean square_diff)
show "variance" variance

\stddev=(sqrt variance)
show "stddev" stddev
)
)

# Now show the stats for a couple of lists.

show_stats [2 4 4 4 5 5 7 9]
show_stats [-11 0 6]


# The output is:

## Show Stats

First call the list_stddev function directly to get the final answer.
list = 2 4 4 4 5 5 7 9
stddev = 2

Now show how we arrive at that answer.
list = 2 4 4 4 5 5 7 9
sum = 40
count = 8
mean = 5
diff = -3 -1 -1 -1 0 0 2 4
square_diff = 9 1 1 1 0 0 4 16
variance = 4
stddev = 2


## Show Stats

First call the list_stddev function directly to get the final answer.
list = -11 0 6
stddev = 7.03957069398096

Now show how we arrive at that answer.
list = -11 0 6
sum = -5
count = 3
mean = -1.66666666666667
diff = -9.33333333333333 1.66666666666667 7.66666666666667
square_diff = 87.1111111111111 2.77777777777778 58.7777777777778
variance = 49.5555555555556
stddev = 7.03957069398096