Advertisement
techno-

Untitled

Jan 8th, 2023
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.59 KB | None | 0 0
  1. type 'a option=
  2.         None
  3.         | Some of 'a;;
  4.  
  5. type maybeAnInt =
  6.         NotAnInt
  7.       | AnInt of int;;
  8.        
  9. let quo x y = match x, y with
  10.         _, AnInt 0-> NotAnInt
  11.         | AnInt m, AnInt n -> AnInt (m/n)
  12.         | _->NotAnInt;;
  13.        
  14. type foo =Foo;;
  15. type boolean = F|T;;
  16.  
  17. let conj a b = match a, b with
  18.         F,_ -> F
  19.       | _,F -> F
  20.       | _->T;;
  21.      
  22.      
  23.      
  24.      
  25. let (&&&) = conj;;
  26.  
  27. let verdadero = T;;
  28.  
  29. let falso = F;;
  30.  
  31. let conj b1 b2 = match b1, b2 with
  32.         verdadero, verdadero ->verdadero
  33.        | _->falso;;
  34.        
  35. type t = T of int;;
  36.  
  37. type tt = L of int | R of int;;
  38.  
  39. type num = F of float | I of int;;
  40.  
  41. type nat = Z | S of nat;;
  42.  
  43. let rec suma x y = match x with
  44.         Z -> y
  45.       | S n -> suma n (S y);;
  46.      
  47. let rec sum n1 = function
  48.         Z-> n1
  49.        |S n2-> sum (S n1) n2;;
  50.  
  51. let rec nat_of_int = function
  52.         0->Z
  53.        | n-> n < 0 then raise (Invalid_argument "nat_of_int")
  54.         else S (nat_of_int(n-1));;
  55.        
  56. let rec nat_of_int = function
  57.         0->Z
  58.        | n->S (nat_of_int(n-1));;
  59.        
  60. let nat_of_int n=
  61.         if n<=0 then raise (Invalid_argument "nat_of_int")
  62.         else nat_of_int n;;
  63.        
  64. type 'a btree=
  65.         E
  66.       | N of 'a * 'a btree * 'a btree;;
  67.      
  68. let l x = N (x, E, E);;
  69.  
  70. let rec num_nodes = function
  71.         E->0
  72.        |N(_,lb,rb)-> 1 + num_nodes lb + num_nodes rb;;
  73.        
  74. let rec height = function
  75.         E->0
  76.        | N(_,lb,rb)-> 1+ max(height lb) (height rb);;
  77.        
  78.  
  79. let rec preorder = function
  80.         E->[]
  81.        |N (x,lb,rb) ->  (x:: preorder lb) @ (preorder rb);;
  82.  
  83. let leaves = function
  84.        E-> []
  85.      | N (r,E,E) -> [r]
  86.      | N (_,l,r) -> leaves l @ leaves r;;
  87.      
  88. type 'a st_tree =
  89.         Leaf of 'a
  90.       | Node of 'a * 'a st_tree * 'a st_tree
  91.      
  92. let rec mirror = function
  93.         Leaf v -> Leaf v
  94.        | Node (v,l,r)-> Node (v,mirror r, mirror l);;
  95.        
  96. type 'a gtree =
  97.         GT of 'a * 'a gtree list;;
  98.        
  99. let rec nngt (GT (_,l))=
  100.        GT (_,l)-> List.fold_left (+) 1 List.map nngt l;;
  101.  
  102. let rec nngt  = function
  103.         GT(_,[])->1
  104.       | GT(v,h::t) -> nngt h + nngt (GT (v,t));;
  105.      
  106. let rec b_of_st = function
  107.         Leaf v->N(v,E,E)
  108.       | Node (v,l,r) -> N(v,b_of_st l, b_of_st r);;
  109.      
  110. let rec st_of_b = function
  111.         E-> raise (Failure "st_of_b")
  112.       | N(v,E,E)-> leaf v
  113.       | N(v,l,r)-> Node (v, st_of_b l, st_of_b r);;        
  114.      
  115. let root = function
  116.         E -> raise (Failure "root")
  117.         | N (r,_,_)->r;;
  118.      
  119.    
  120.      
  121.      
  122.      
  123.      
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement