Skip to content

Instantly share code, notes, and snippets.

@relrod
Last active June 3, 2016 00:10
Show Gist options
  • Save relrod/f9d4ddc9b7905198e6bb5241abfe9f01 to your computer and use it in GitHub Desktop.
Save relrod/f9d4ddc9b7905198e6bb5241abfe9f01 to your computer and use it in GitHub Desktop.
Definition digit_to_string n : string :=
match n with
| 0%Z => "0"
| 1%Z => "1"
| 2%Z => "2"
| 3%Z => "3"
| 4%Z => "4"
| 5%Z => "5"
| 6%Z => "6"
| 7%Z => "7"
| 8%Z => "8"
| 9%Z => "9"
| _ => ""
end.
Fixpoint z_to_string' n z acc : string :=
match n with
| 0 => acc
| S n' =>
let acc' := digit_to_string (Zmod z 10) ++ acc
in match (Zdiv z 10) with
| Z0 => acc'
| z' => z_to_string' n' z' acc'
end
end.
Definition z_to_string z :=
if Z.geb z 0
then z_to_string' (Z.to_nat z) z ""
else let z' := Zabs z
in "-" ++ z_to_string' (Z.to_nat z') z' "".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment