Advertisement
kator

Ocaml lab4

Mar 20th, 2019
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.48 KB | None | 0 0
  1. open Printf
  2.  
  3. let print_list cf l =
  4.   let rec string_of_list l =
  5.     match l  with
  6.     [] -> ""
  7.     | h::tail -> cf h^";"^string_of_list tail
  8.     in Printf.printf "[%s]\n" (string_of_list l);;
  9.  
  10. print_list string_of_int [1;2;3;];;
  11.  
  12. print_list string_of_int [1;2;3;];;
  13. print_list (fun x -> "\""^x^"\"") ["ala";"kot"];;
  14.  
  15. (1,"ala",10.);;
  16. type foo =
  17.   Nic
  18.   | Liczba of int
  19.   | Para of float * string
  20.   | Tekst of string;;
  21.  
  22. let x : foo = Para(3.,"ala");;
  23. let y = Nic;;
  24. let z = Tekst " i kot";;
  25.  
  26.  
  27. let f = function
  28.   Nic -> 0
  29.   | Liczba i -> i
  30.   | Para (x,y) -> int_of_float x
  31. | Tekst s -> printf "%s\n" s; 1;;
  32.  
  33. type 'a btree =  
  34.   Leaf of 'a
  35. | Node of 'a btree * 'a btree ;;
  36.  
  37. Leaf 1;;
  38. (*
  39. let dab = Node ( Node(Leaf 1, Leaf 2),
  40.                 Leaf 3);;
  41. *)
  42.  
  43. let list = Leaf 10;;
  44. let sosna = Node ( Node(Leaf "a", Leaf "ab"),
  45.                   Leaf "c");;
  46.  
  47. let print_list cf l =
  48.   let rec string_of_list l =
  49.     match l  with
  50.     [] -> ""
  51.     | h::tail -> cf h^";"^string_of_list tail
  52.     in Printf.printf "[%s]\n" (string_of_list l);;
  53.  
  54. print_list string_of_int [1;2;3;];;
  55.  
  56. let print_tree cf d : 'a btree =
  57.   let rec string_of_tree d =
  58.     match d with
  59.     Leaf f -> (cf f)
  60.     | Node (n1,n2) -> " N("^string_of_tree n1^";"^string_of_tree n2^") "
  61.   in printf "TREE -> [%s]\n" (string_of_tree d); d;;
  62.  
  63. print_tree (fun x -> "'"^x^"'") sosna;;
  64.  
  65. let dab = Node ( Node(Leaf 2, Leaf 1),
  66.                   Leaf 3);;
  67.  
  68.  
  69. print_tree string_of_int dab;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement