Guest User

Untitled

a guest
May 5th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.47 KB | None | 0 0
  1. module type TRIP =
  2.     sig
  3.         type trip;;
  4.         val create_trip : int -> int -> trip list -> trip list
  5.         val delete_trip : int -> trip list -> trip list
  6.         val list_trip : int -> trip list -> trip list
  7.  
  8.     end
  9.  
  10. module Trip : TRIP =
  11.     struct
  12.         type trip = (int * int);;
  13.         let rec create_trip digit speed listy = (digit, speed)::listy;;
  14.         let rec delete_trip index listy = [];;
  15.         let rec list_trip n = function
  16.             | (digit, speed)::tail -> Printf.printf "%d %d\n" digit speed; (digit, speed)::(list_trip n tail)
  17.             | [] -> []
  18.     end;;
  19.  
  20. let rec matcher = function
  21.     | str -> match (List.hd (Str.split (Str.regexp "[ \t]+") str)) with
  22.         | "create" -> Printf.printf "create\n"
  23.         | "delete" -> Printf.printf "delete\n"
  24.         | "list" -> Printf.printf "list\n"
  25.         | _ -> Printf.printf "WTF bro\n"
  26. ;;
  27.  
  28. let rec debugger = function
  29.     | head::tail -> Printf.printf "%s\n" head; debugger tail
  30.     | [] -> []
  31. ;;
  32.  
  33. (* let lines = ref [] in
  34. try
  35.   while true; do
  36.     lines := (matcher (read_line ())) :: !lines
  37.   done; !lines
  38. with End_of_file ->
  39.   debugger (List.rev !lines) ;; *)
  40.  
  41. type mytype = Trip.trip list;;
  42.  
  43. let read_lines name : Trip.trip list =
  44. let ic = open_in name in
  45. let try_read () =
  46.     try Some  (matcher (read_line ())) with End_of_file -> None in
  47. let rec loop acc = match try_read () with
  48.     | Some s -> loop (s :: acc)
  49.     | None -> close_in ic; List.rev acc in
  50.   loop []
Add Comment
Please, Sign In to add comment