kmj
09-10-2002, 10:50 AM
let rec dot_product a b =
match a with
[] -> (match b with [] -> 0)
| ah::at ->
(match b with
bh::bt -> ah*bh + dot_product at bt);;
It returns warnings due to "unexhausted matches" which is fine for right now, because those will occur when a person tries to compute the dot product of two incongruent vectors; and anyway, I haven't learned how to raise my own exceptions yet. In fact, this function displays the full extent of my current knowledge of O'Caml. Clearly, I have alot to learn. It also displays the power of functional programming; how much can be done with so little syntax.
Anyway, to use this function, simply supply two arrays of equal lengths, which it will treat as vectors...
# dot_product [1;2;3] [4;0;1];;
- : int = 7
This is a starter to dru's competition, if you haven't noticed. :)
match a with
[] -> (match b with [] -> 0)
| ah::at ->
(match b with
bh::bt -> ah*bh + dot_product at bt);;
It returns warnings due to "unexhausted matches" which is fine for right now, because those will occur when a person tries to compute the dot product of two incongruent vectors; and anyway, I haven't learned how to raise my own exceptions yet. In fact, this function displays the full extent of my current knowledge of O'Caml. Clearly, I have alot to learn. It also displays the power of functional programming; how much can be done with so little syntax.
Anyway, to use this function, simply supply two arrays of equal lengths, which it will treat as vectors...
# dot_product [1;2;3] [4;0;1];;
- : int = 7
This is a starter to dru's competition, if you haven't noticed. :)