REVISED: Sunday, October 5, 2025
1. OCAML VECTORS
(*
============================
Lesson 1: Vectors in OCaml
============================
Objective: Represent vectors and implement basic operations:
1. Vector addition
2. Scalar multiplication
3. Dot product
4. Magnitude (L2 norm)
*)
(* -----------------------------
1. Vector type
----------------------------- *)
(* We'll represent a vector as a list of floats. *)
type vector = float list
(* -----------------------------
2. Vector addition
----------------------------- *)
(* Adds two vectors element-wise.
Raises an error if the vectors have different lengths. *)
let rec vector_add v1 v2 =
match v1, v2 with
| [], [] -> [] (* base case: both vectors empty *)
| x::xs, y::ys -> (x +. y) :: vector_add xs ys (* add head elements, recurse on tails *)
| _ -> failwith "Vectors must be the same length"
(* -----------------------------
3. Scalar multiplication
----------------------------- *)
(* Multiply each element of a vector by a scalar *)
let rec scalar_mul scalar v =
match v with
| [] -> [] (* base case *)
| x::xs -> (scalar *. x) :: scalar_mul scalar xs
(* -----------------------------
4. Dot product
----------------------------- *)
(* Computes the sum of products of corresponding elements *)
let rec dot_product v1 v2 =
match v1, v2 with
| [], [] -> 0.0 (* base case *)
| x::xs, y::ys -> (x *. y) +. dot_product xs ys
| _ -> failwith "Vectors must be the same length"
(* -----------------------------
5. Magnitude (L2 norm)
----------------------------- *)
(* Square root of the sum of squares of elements *)
let magnitude v =
sqrt (dot_product v v)
(* -----------------------------
6. Examples / Testing
----------------------------- *)
let v1 = [1.0; 2.0; 3.0]
let v2 = [4.0; 5.0; 6.0]
(* Vector addition *)
let v_add = vector_add v1 v2
let () =
Printf.printf "v1 + v2 = [%s]\n"
(String.concat "; " (List.map string_of_float v_add))
(* Scalar multiplication *)
let v_scaled = scalar_mul 2.0 v1
let () =
Printf.printf "2 * v1 = [%s]\n"
(String.concat "; " (List.map string_of_float v_scaled))
(* Dot product *)
let dp = dot_product v1 v2
let () = Printf.printf "v1 . v2 = %f\n" dp
(* Magnitude *)
let mag_v1 = magnitude v1
let () = Printf.printf "|v1| = %f\n" mag_v1
2. CONCLUSION
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
OCaml version: The OCaml toplevel, version 5.3.0
Coq-LSP version: 0.2.3
Loading personal and system profiles took 1154ms.
PS C:\Users\User> ocaml C:\AI2025\lesson1.ml
v1 + v2 = [5.; 7.; 9.]
2 * v1 = [2.; 4.; 6.]
v1 . v2 = 32.000000
|v1| = 3.741657
PS C:\Users\User>
3. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.