Facebook
From Round Peafowl, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 245
  1. module type Container =
  2. sig
  3.         type 'a t
  4.         exception Empty of string
  5.        
  6.         val create: unit -> 'a t
  7.         val put: 'a * 'a t -> unit
  8.         val take: 'a t -> 'a
  9. end;;
  10.  
  11. module ContainerList =
  12. struct
  13.         type 'a t = {mutable a : 'a list }
  14.         exception Empty of string
  15.        
  16.         let create() = { a = [] }
  17.  
  18.  
  19.         let put (e, c) =
  20.                 match c.a with
  21.                 | [] -> begin c.a <- [e] end
  22.                 | h::t -> begin c.a <- c.a@[e] end
  23.  
  24.         let take c =
  25.                 match c.a with
  26.                 | [] -> raise (Empty "e")
  27.                 | h::t -> begin c.a <- t ; h end
  28.  
  29.        
  30. end;;
  31.  
  32. let cL = ContainerList.create();;
  33. ContainerList.put(1, cL);;
  34. ContainerList.put(2, cL);;
  35. ContainerList.put(3, cL);;
  36.  
  37. ContainerList.take cL;;