Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. haskCoq :: [a] -> List a
  2. haskCoq [] = Nil
  3. haskCoq (h : hs) = Cons h (haskCoq hs)
  4.  
  5. coqNat :: P.Int -> Nat
  6. coqNat n
  7. | n P.== 0 = O
  8. | P.otherwise = S (coqNat (n P.- 1))
  9.  
  10.  
  11. wordsWhen :: (P.Char -> P.Bool) -> P.String -> [P.String]
  12. wordsWhen p s = case L.dropWhile p s of
  13. "" -> []
  14. s' -> w : wordsWhen p s''
  15. where (w, s'') = L.break p s'
  16.  
  17.  
  18. balfun :: [(Cand, Nat)] -> (Ballot Cand)
  19. balfun [(A, b1), (B, b2), (C, b3), (D, b4)] = f where
  20. f :: Cand -> Nat
  21. f A = b1
  22. f B = b2
  23. f C = b3
  24. f D = b4
  25.  
  26.  
  27. main :: IO ()
  28. main = do
  29. r <- getContents
  30. let votes = P.map (balfun P..
  31. L.sort P.. P.map (\(y, z) -> (y, coqNat z)) P..
  32. P.map (\x -> P.read x :: (Cand, P.Int)) P..
  33. wordsWhen (P.== ';')) P.. P.lines P.$ r
  34. P.print P.$ schulze_winners_pf (haskCoq votes)
  35.  
  36. I am trying to convert the Haskell code into Ocaml and below is my implementation.
  37.  
  38.  
  39. let rec ocamlcoq l =
  40. match l with
  41. | [] -> Nil
  42. | h :: t -> Cons (h, (ocamlcoq t))
  43.  
  44. let rec ocamlnat n =
  45. match n with
  46. | 0 -> O
  47. | _ -> S (ocamlnat (n -1))
  48.  
  49. let balfun l =
  50. match l with
  51. | [(A, b1); (B, b2); (C, b3); (D, b4)] ->
  52. let f A = b1 in
  53. let f B = b2 in
  54. let f C = b3 in
  55. let f D = b4 in f
  56. | _ -> failwith "failed to match pattern"
  57.  
  58.  
  59. let input_line_opt ic =
  60. try Some (input_line ic)
  61. with End_of_file -> None
  62.  
  63. let read_lines ic =
  64. let rec aux acc =
  65. match input_line_opt ic with
  66. | Some line -> aux (line::acc)
  67. | None -> (List.rev acc)
  68. in
  69. aux []
  70.  
  71. let lines_of_file filename =
  72. let ic = open_in filename in
  73. let lines = read_lines ic in
  74. close_in ic;
  75. lines
  76.  
  77.  
  78. let process_input filename =
  79. let lines = lines_of_file filename in
  80. List.map (fun x -> Str.split (Str.regexp ";") x) lines
  81.  
  82. Input file is
  83.  
  84. (A,3);(B,1);(C,2);(D,4)
  85. (A,1);(B,0);(C,4);(D,3)
  86. (A,3);(B,1);(C,2);(D,4)
  87. (A,2);(B,3);(C,3);(D,4)
  88. (A,3);(B,1);(C,2);(D,4)
  89. (A,1);(B,2);(C,3);(D,4)
  90. (A,1);(B,2);(C,3);(D,4)
  91. (A,1);(B,2);(C,2);(D,4)
  92. (A,1);(B,2);(C,2);(D,4)
  93. (A,1);(B,3);(C,2);(D,4)
  94.  
  95. utop[34]> process_input "example-votes.txt";;
  96. - : string BatList.t BatList.t =
  97. [["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,0)"; "(C,4)"; "(D,3)"];
  98. ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,2)"; "(B,3)"; "(C,3)"; "(D,4)"];
  99. ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"];
  100. ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"];
  101. ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,3)"; "(C,2)"; "(D,4)"]]
  102.  
  103. I am looking for function to convert "(A,3)" : string to (A, 3) : cand * int
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement