Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local
- fun drop_string n st = foldr (fn (x,y) => str(x) ^ y) "" (List.drop((explode st),n))
- fun index _ [] = ~1
- | index n (x::xs) = if n = x then 0 else
- let
- val l = (index n xs)
- in
- if (l = ~1) then l else l + 1
- end
- fun fraction2RepeatingDecimal_aux 0 _ _ output = output
- | fraction2RepeatingDecimal_aux n m lst output =
- let
- val quation = ((n*10) div m)
- val reminder = ((n*10) - (quation*m))
- val l = (index reminder lst)
- in
- if (l = ~1) then
- fraction2RepeatingDecimal_aux reminder m (lst @ [reminder]) (output ^ Int.toString(quation))
- else if String.substring(output, l, 1) <> Int.toString(quation) then
- String.substring(output, 0, l + 1) ^ "(" ^ Int.toString(quation) ^ ")"
- else String.substring(output, 0, l) ^ "(" ^ (drop_string l output) ^ ")"
- end
- in
- fun fraction2RepeatingDecimal 0 _ = "0.0"
- | fraction2RepeatingDecimal n m = "0." ^ (fraction2RepeatingDecimal_aux n m [] "")
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement