Non-decimal radices/Convert: Difference between revisions
support 0, fix non-exhaustive matching, use modern API
(→{{header|Quackery}}: rewrote to match task requirements) |
(support 0, fix non-exhaustive matching, use modern API) |
||
Line 2,524:
- : int = 26
A real base conversion
let tab = "0123456789abcdefghijklmnopqrstuvwxyz" in
let rec aux x l =
then tab.[x] :: l
else aux (x / b) (tab.[x mod b] :: l)
in
String.of_seq (List.to_seq (aux n []))
let basen_to_int b ds : int =
▲<syntaxhighlight lang="ocaml">let to_base b v =
let
▲ a
|
| _ -> invalid_arg "unkown digit"
in
▲ List.fold_left (fun n k -> n * b + k) 0 ds
▲ '0'..'9' -> int_of_char c - int_of_char '0'
▲ | 'a'..'z' -> int_of_char c - int_of_char 'a' + 10
Example:
<pre>
#
- : string = "
#
- : int =
</pre>
|