(* a. factorial *)
fun factorial n = if n = 0 then 1
else n * factorial(n-1)
(* b. cycle:
each cycle takes a list [a1,a2,a3,...,an] and produces [a2,a3,...,an,a1]
*)
fun cycleonce [] = []
| cycleonce (x :: xs) = xs @ [x]
fun cycle i l = if i = 0 then l
else cycle (i-1) (cycleonce l)
(* c. duplicate each element of a list *)
fun dup [] = []
| dup (x :: xs) = x :: x :: dup xs
(* d. compute x to the i *)
fun power x i = if i = 0 then 1.0
else x * power x (i-1)
(* e. max of a list of reals *)
fun maxreals [] = 0.0
| maxreals (x :: xs) = let val m = maxreals xs
in if (x > m) then x else m
end
(****************************************************************************)