ghuysmans / roman

OCaml module and command line tool to convert a roman numeral to an integer and vice versa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Roman - convert roman numerals to integer

This repository provides an OCaml module and minimal command line tool to convert modern roman numerals to integer and vice versa:

$ roman mmxv
2015
$ roman 1234
mccxxxiv

The command line tool recognizes integer and roman numerals and emits it after conversion to the opposite format.

The module accepts the syntax for modern roman numerals as defined by Wikipedia and detects illegal syntax like the following:

im
xcc
ic
imm
mxm
viiii
ivi

This little exercise was prompted by an article Radikale Objektorientierung: Teil 2: Verhalten prinzipiengeleitet verfeinern in OBJEKTspektrum issue 2/12015 that used conversion of roman numerals as an example for OO design. The code from that article is available on GitHub.

Build and Test

$ make
$ make test
$ make install

The implementation is in roman.mll and uses ocamllex to build an automaton for recognising roman numerals. To use as a module, simply add it as an ocamlfind dependency in your own project.

Syntax

For the syntax of roman numerals recognised by the module, take a look at roman.mll where it is encoded as a regular expression:

(M|MM|MMM)?
(D?(C|CC|CCC)?|CD|CM)?
(L?(X|XX|XXX)?|XL|XC)?
(V?(I|II|III)?|IV|IX)?

Author

Christian Lindig lindig@gmail.com

License

This code is licensed under the BSD 2-clause license.

About

OCaml module and command line tool to convert a roman numeral to an integer and vice versa

License:BSD 2-Clause "Simplified" License


Languages

Language:OCaml 98.4%Language:Makefile 1.6%