Advertisement
razvanth21

Untitled

May 16th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.33 KB | None | 0 0
  1. let headoflist lst = match lst with
  2.     | [] -> failwith "Empty list"
  3.     | [h] -> h
  4.     | h :: t -> h
  5.  
  6. let tailoflist lst = match lst with
  7.     | [] -> failwith "Empty list"
  8.     | [h] -> failwith "One element only"
  9.     | h :: t -> t;;
  10.  
  11. let rotate_left lst = match lst with
  12.     | [] -> []
  13.     | h :: t -> t @ [h];;
  14.    
  15. let rec last = function
  16.     | [] -> -1
  17.     | [h] -> h
  18.     | h :: t -> last t;;
  19.  
  20. let rec maxoflist lst = match lst with
  21.     | [] -> failwith "Empty list"
  22.     | [h] -> h
  23.     | h :: t -> max h (maxoflist t);;
  24.  
  25. let u x y = x + y;;
  26. let f x y = 2 * x + y;;
  27.  
  28. let rec apply f ac lst = match lst with
  29.     | [] -> 0
  30.     | [h] -> (f h ac)
  31.     | h :: t -> (f h (apply f ac t));;
  32.  
  33. type nrcomplex = Real of float | Complex of (float * float);;
  34.  
  35. let add_complex x y = match (x, y) with
  36.     | (Real x, Real y) -> Real(x +. y)
  37.     | (Real x, Complex(ax, ay)) -> Complex(x +. ax, ay)
  38.     | (Complex(ax, ay), Real y) -> Complex(ax, y +. ay)
  39.     | (Complex(ax, ay), Complex(bx, by)) -> Complex(ax +. bx, ay +. by);;
  40.    
  41. let mul_complex x y = match (x, y) with
  42.     | (Real x, Real y) -> Real(x *. y)
  43.     | (Real x, Complex(ax, ay)) -> Complex(x *. ax, x *. ay)
  44.     | (Complex(ax, ay), Real y) -> Complex(y *. ax, y *. ay)
  45.     | (Complex(ax, ay), Complex(bx, by)) -> Complex((ax *. bx -. ay *. by), (ax *. by +. bx *. ay));;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement