let x = 2
let square x =
x*x
let mult x y =
x * y
let number x =
match x with
0 -> "null"
| 1 -> "eins"
| 2 -> "zwei"
| 3 -> "drei"
| _ -> "ganz viel"
let rec mul17 xs =
match xs with
[] -> []
| y :: ys -> 17*y :: mul17 ys
let rec greater7 xs =
match xs with
[] -> []
| x :: xs -> if x > 7 then x :: greater7 xs else greater7 xs
let rec sum xs =
match xs with
[] -> 0
| x :: xs -> x + sum xs
let rec map f xs =
match xs with
[] -> []
| x :: xs -> f x :: map f xs
let rec myfold c n xs =
match xs with
[] -> n
| x :: xs -> c x (myfold c n xs)
let rec filter p xs =
match xs with
[] -> []
| x :: xs ->
if p x
then x :: filter p xs
else filter p xs
let compose f g x =
f (g x)
let flip f x y =
f y x
let twice f x =
f (f x)
type number = Int of int | Float of float | Error
let nsquare x =
match x with
Int i -> Int (i*i)
| Float f -> Float (f*.f)
| Error -> Error
type 'a ltree = Leaf of 'a | Branch of 'a ltree * 'a ltree
let rec lmap f t =
match t with
Leaf x -> Leaf (f x)
| Branch (l, r) -> Branch (lmap f l, lmap f r)
type exp =
Const of float
| Var of string
| Add of exp * exp
| Mul of exp * exp
| Div of exp * exp
| Sin of exp
let ifac n =
let acc = ref 1 in
let rec loop i =
if i <= 0
then !acc
else
begin
acc := !acc * i;
loop (i-1)
end
in
loop n
type point2d =
{ mutable x : float ; mutable y : float }
let translate p dx dy =
begin
p.x <- p.x +. dx;
p.y <- p.y +. dy;
end
type pointmethods =
{ move : float -> float -> unit;
scale : float -> unit;
getx : unit -> float;
gety : unit -> float;
}
let newpoint x y =
let p = { x=x; y=y; } in
{ move = (fun dx dy -> (p.x <- p.x +. dx; p.y <- p.y +. dy));
scale = (fun d -> (p.x <- d*.p.x; p.y <- d*.p.y));
getx = (fun () -> p.x);
gety = (fun () -> p.y);
}
let p = newpoint 20. 30.
p.scale 0.1
(*
f : a -> b -> c
g : d -> b
h : a -> d -> c
h x y = f x (g y)
---------
g : e -> a
h : e -> b -> c
h = compose f g
*)