The "function" keyword creates a function that takes one parameter and
automatically pattern matches against it. It's the same as:
Fun x -> match x with
.
So yeah, variance ends up being a function that accepts a list, if it's
empty you get an exception, and if its not it calculates the variance
_____
From: ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com
[mailto: ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com] On Behalf Of Joel Reymont
Sent: Sunday, February 25, 2007 8:09 PM
To: ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com
Subject: "ocaml_beginners"::[] What does this do?
Folks,
I don't understand the use of "function" in the following chunk of
code. Could someone explain it to me please? Does the code define
variance as a function of one argument?
let variance =
let aux (m_k, s_k, k) x_k =
let m_k2 = m_k +. (x_k -. m_k) /. k in
(m_k2, s_k +. (x_k -. m_k) *. (x_k -. m_k2), k +. 1.) in
function [] -> invalid_arg "variance" | x1::t ->
let (_, s, n2) = List.fold_left aux (x1, 0., 2.) t in
s /. (n2 -. 2.);;
Thanks, Joel
--
http://wagerlabs. <http://wagerlabs.com/> com/
[Non-text portions of this message have been removed]
.