add lab 07 doc comments
This commit is contained in:
2 files changed
+42
No files matched your search
@@ -1,14 +1,28 @@
|
||||
(** A lazy infinite stream containing values of type ['a]. *)
|
||||
type 'a lazystream = Cons of 'a * 'a lazystream Lazy.t
|
||||
|
||||
(** [from n] creates an infinite lazy stream starting at [n]
|
||||
* and increasing by [1.] for each subsequent element.
|
||||
*)
|
||||
let rec from n = Cons (n, lazy (from (n +. 1.)))
|
||||
|
||||
(** [take n s] returns a list containing the first [n] elements
|
||||
* of lazy stream [s].
|
||||
* If [n <= 0], it returns the empty list.
|
||||
*)
|
||||
let rec take n (Cons (h, t)) =
|
||||
if n <= 0 then []
|
||||
else h :: take (n - 1) (Lazy.force t)
|
||||
|
||||
(** [map f s] returns a new lazy stream where function [f]
|
||||
* is applied to every element of stream [s].
|
||||
*)
|
||||
let rec map f (Cons (h, t)) =
|
||||
Cons (f h, lazy (map f (Lazy.force t)))
|
||||
|
||||
(** [fact n] computes the factorial of [n].
|
||||
* This function assumes [n] is a non-negative floating-point integer value.
|
||||
*)
|
||||
let fact n =
|
||||
let rec fact' acc i =
|
||||
if i = 0. then acc
|
||||
@@ -16,13 +30,25 @@ let fact n =
|
||||
in
|
||||
fact' 1. n;;
|
||||
|
||||
(** [fold_left f acc l] applies function [f] to each element of list [l]
|
||||
* from left to right, carrying an accumulator [acc].
|
||||
* The function [f] takes the current accumulator and element
|
||||
* and produces a new accumulator.
|
||||
*)
|
||||
let rec fold_left f acc l =
|
||||
match l with
|
||||
| [] -> acc
|
||||
| a :: l' -> fold_left f (f acc a) l';;
|
||||
|
||||
(** [exp_terms x] returns an infinite lazy stream of terms in the
|
||||
* Taylor series expansion of [e^x], where each term is
|
||||
* [(x ** n) /. fact n] for [n = 0., 1., 2., ...].
|
||||
*)
|
||||
let exp_terms x = map (fun n -> (x**n) /. (fact n)) @@ from 0.;;
|
||||
|
||||
(** [exp n x] approximates [e^x] by summing the first [n]
|
||||
* terms of the Taylor series expansion for [e^x].
|
||||
*)
|
||||
let exp n x = fold_left (+.) 0. (take n @@ exp_terms x);;
|
||||
|
||||
exp 20 1.1;;
|
||||
@@ -1,15 +1,31 @@
|
||||
(** An infinite stream containing values of type ['a],
|
||||
* where the tail is produced by a thunk.
|
||||
*)
|
||||
type 'a infstream = Cons of 'a * (unit -> 'a infstream)
|
||||
|
||||
(** [take n s] returns a list containing the first [n] elements
|
||||
* of infinite stream [s].
|
||||
* If [n <= 0], it returns the empty list.
|
||||
*)
|
||||
let rec take n (Cons (h, t)) =
|
||||
if n <= 0 then []
|
||||
else h :: take (n - 1) (t ())
|
||||
|
||||
(** [from n] creates an infinite stream of integers starting at [n]
|
||||
* and increasing by [1] for each subsequent element.
|
||||
*)
|
||||
let rec from n = Cons (n, fun () -> from (n + 1))
|
||||
|
||||
(** [filter f s] returns a new infinite stream containing only
|
||||
* the elements of stream [s] that satisfy predicate [f].
|
||||
*)
|
||||
let rec filter f (Cons (h, t)) =
|
||||
if f h then Cons (h, fun () -> filter f (t ()))
|
||||
else filter f (t ())
|
||||
|
||||
(** [primes] is an infinite stream of prime numbers generated
|
||||
* using the sieve of Eratosthenes.
|
||||
*)
|
||||
let primes =
|
||||
let rec sieve (Cons (h, t)) =
|
||||
Cons (h, fun () ->
|
||||
|
||||
Reference in new issue
Block a user