Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- haskCoq :: [a] -> List a
- haskCoq [] = Nil
- haskCoq (h : hs) = Cons h (haskCoq hs)
- coqNat :: P.Int -> Nat
- coqNat n
- | n P.== 0 = O
- | P.otherwise = S (coqNat (n P.- 1))
- wordsWhen :: (P.Char -> P.Bool) -> P.String -> [P.String]
- wordsWhen p s = case L.dropWhile p s of
- "" -> []
- s' -> w : wordsWhen p s''
- where (w, s'') = L.break p s'
- balfun :: [(Cand, Nat)] -> (Ballot Cand)
- balfun [(A, b1), (B, b2), (C, b3), (D, b4)] = f where
- f :: Cand -> Nat
- f A = b1
- f B = b2
- f C = b3
- f D = b4
- main :: IO ()
- main = do
- r <- getContents
- let votes = P.map (balfun P..
- L.sort P.. P.map (\(y, z) -> (y, coqNat z)) P..
- P.map (\x -> P.read x :: (Cand, P.Int)) P..
- wordsWhen (P.== ';')) P.. P.lines P.$ r
- P.print P.$ schulze_winners_pf (haskCoq votes)
- I am trying to convert the Haskell code into Ocaml and below is my implementation.
- let rec ocamlcoq l =
- match l with
- | [] -> Nil
- | h :: t -> Cons (h, (ocamlcoq t))
- let rec ocamlnat n =
- match n with
- | 0 -> O
- | _ -> S (ocamlnat (n -1))
- let balfun l =
- match l with
- | [(A, b1); (B, b2); (C, b3); (D, b4)] ->
- let f A = b1 in
- let f B = b2 in
- let f C = b3 in
- let f D = b4 in f
- | _ -> failwith "failed to match pattern"
- let input_line_opt ic =
- try Some (input_line ic)
- with End_of_file -> None
- let read_lines ic =
- let rec aux acc =
- match input_line_opt ic with
- | Some line -> aux (line::acc)
- | None -> (List.rev acc)
- in
- aux []
- let lines_of_file filename =
- let ic = open_in filename in
- let lines = read_lines ic in
- close_in ic;
- lines
- let process_input filename =
- let lines = lines_of_file filename in
- List.map (fun x -> Str.split (Str.regexp ";") x) lines
- Input file is
- (A,3);(B,1);(C,2);(D,4)
- (A,1);(B,0);(C,4);(D,3)
- (A,3);(B,1);(C,2);(D,4)
- (A,2);(B,3);(C,3);(D,4)
- (A,3);(B,1);(C,2);(D,4)
- (A,1);(B,2);(C,3);(D,4)
- (A,1);(B,2);(C,3);(D,4)
- (A,1);(B,2);(C,2);(D,4)
- (A,1);(B,2);(C,2);(D,4)
- (A,1);(B,3);(C,2);(D,4)
- utop[34]> process_input "example-votes.txt";;
- - : string BatList.t BatList.t =
- [["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,0)"; "(C,4)"; "(D,3)"];
- ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,2)"; "(B,3)"; "(C,3)"; "(D,4)"];
- ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"];
- ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"];
- ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,3)"; "(C,2)"; "(D,4)"]]
- 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