Advertisement
Guest User

Untitled

a guest
May 21st, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local
  2.     fun drop_string n st = foldr (fn (x,y) => str(x) ^ y) "" (List.drop((explode st),n))   
  3.     fun index _ [] = ~1
  4.         | index n (x::xs) = if n = x then 0 else
  5.             let
  6.                 val l = (index n xs)
  7.             in
  8.                 if (l = ~1) then l else l + 1
  9.             end
  10.     fun fraction2RepeatingDecimal_aux 0 _ _ output = output
  11.         | fraction2RepeatingDecimal_aux n m lst output =
  12.             let
  13.                 val quation = ((n*10) div m)
  14.                 val reminder = ((n*10) - (quation*m))
  15.                 val l = (index reminder lst)
  16.             in
  17.                 if (l = ~1) then
  18.                     fraction2RepeatingDecimal_aux reminder m (lst @ [reminder]) (output ^ Int.toString(quation))
  19.                 else if String.substring(output, l, 1) <> Int.toString(quation) then
  20.                     String.substring(output, 0, l + 1) ^ "(" ^ Int.toString(quation) ^ ")"
  21.                 else String.substring(output, 0, l) ^ "(" ^ (drop_string l output) ^ ")"
  22.             end            
  23. in
  24.     fun fraction2RepeatingDecimal 0 _ = "0.0"
  25.         | fraction2RepeatingDecimal n m = "0." ^ (fraction2RepeatingDecimal_aux n m [] "")
  26. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement