caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* is there a Roman Numeral library
@ 2008-03-14 16:50 Ashish Agarwal
  2008-03-14 20:29 ` [Caml-list] " Dave Benjamin
  2008-03-16  6:20 ` Nathan Mishra Linger
  0 siblings, 2 replies; 4+ messages in thread
From: Ashish Agarwal @ 2008-03-14 16:50 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 141 bytes --]

Hi all. Does anyone have an OCaml libary for working with Roman Numerals?
Probably easy to write but am hoping it already exists. Thank you.

[-- Attachment #2: Type: text/html, Size: 149 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] is there a Roman Numeral library
  2008-03-14 16:50 is there a Roman Numeral library Ashish Agarwal
@ 2008-03-14 20:29 ` Dave Benjamin
  2008-03-16  6:20 ` Nathan Mishra Linger
  1 sibling, 0 replies; 4+ messages in thread
From: Dave Benjamin @ 2008-03-14 20:29 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: caml-list

On Fri, 14 Mar 2008, Ashish Agarwal wrote:

> Hi all. Does anyone have an OCaml libary for working with Roman Numerals?
> Probably easy to write but am hoping it already exists. Thank you.

This is by no means a complete library, but I translated some conversion 
functions here:

http://pleac.sourceforge.net/pleac_ocaml/numbers.html#AEN88

Dave


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] is there a Roman Numeral library
  2008-03-14 16:50 is there a Roman Numeral library Ashish Agarwal
  2008-03-14 20:29 ` [Caml-list] " Dave Benjamin
@ 2008-03-16  6:20 ` Nathan Mishra Linger
  2008-03-16 23:25   ` Ashish Agarwal
  1 sibling, 1 reply; 4+ messages in thread
From: Nathan Mishra Linger @ 2008-03-16  6:20 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 611 bytes --]

Hi Ashish,

Here is something I wrote a while back.  Let me know if it works out for
you.

Nathan

2008/3/14 Ashish Agarwal <agarwal1975@gmail.com>:

> Hi all. Does anyone have an OCaml libary for working with Roman Numerals?
> Probably easy to write but am hoping it already exists. Thank you.
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>

[-- Attachment #1.2: Type: text/html, Size: 1164 bytes --]

[-- Attachment #2: roman.ml --]
[-- Type: text/plain, Size: 2426 bytes --]

(*
        roman numeral conversions
 *)

(******************************************************************************)
(* arabic <== roman *)

exception BadNumeral of char

let eval = function
  | 'I' ->         1
  | 'V' ->         5
  | 'X' ->        10
  | 'L' ->        50
  | 'C' ->       100
  | 'D' ->       500
  | 'M' ->      1000
  | c   -> raise(BadNumeral(c))

let arabic x = 
  let n = String.length x in
  let rec loop i v p =
        if i < n then
          let c = eval (String.get x i) in
          loop (i+1) (if p < c then v-p else v+p) c
        else v + p
  in
    if n > 0
    then let v = eval (String.get x 0) in loop 1 0 v
    else 0

(******************************************************************************)
(* (arabic ==> roman) abstract interpretation on SIZES *)

let numerals_size = 7

let digit_size = function
  | 0 -> 0 | 1 -> 1 | 2 -> 2 | 3 -> 3 | 4 -> 2
  | 5 -> 1 | 6 -> 2 | 7 -> 3 | 8 -> 4 | 9 -> 2
  | _ -> assert false

let rec count_size k = function
  | (0,_) -> k
  | (n,j) ->
        if j >= 3
        then count_size (digit_size (n mod 10) + k) (n/10,j-2)
        else n+k

let roman_size n =
        if n < 0 then 0 else
        count_size 0 (n,numerals_size)

(******************************************************************************)
(* arabic ==> roman *)

exception Negative

let numerals = ['I';'V';'X';'L';'C';'D';'M']

let roman n =
  let size = roman_size n in
  let x = String.make size 'M' in
  let ( ++ ) c k = String.set x k c; k-1 in
  let digit d one five ten k =
    match d with
    | 0 -> k
    | 1 ->  one ++ k
    | 2 ->  one ++ (one ++ k)
    | 3 ->  one ++ (one ++ (one ++ k))
    | 4 ->  one ++ (five ++ k)
    | 5 -> five ++ k
    | 6 -> five ++ (one ++ k)
    | 7 -> five ++ (one ++ (one ++ k))
    | 8 -> five ++ (one ++ (one ++ (one ++ k)))
    | 9 ->  one ++ (ten ++ k)
    | _ -> raise Negative
  in
  let rec count k = function
   | 0,_ -> ()
   | n,[_] -> ()
   | n,(one :: five :: ((ten :: _) as next)) ->
        count (digit (n mod 10) one five ten k) (n/10, next)
   | _ -> assert false
  in
  count (size-1) (n,numerals); x

(******************************************************************************)
(* debugging *)

let debug = false

let () = if debug then begin
  let rec upto i j = if i > j then [] else i :: upto (i+1) j in
  let test n = arabic (roman n) = n in
  let _ = List.for_all test (upto 0 5000) in
  ()
end


[-- Attachment #3: roman.mli --]
[-- Type: application/octet-stream, Size: 106 bytes --]


exception BadNumeral of char

val arabic : string -> int

exception Negative

val roman : int -> string


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] is there a Roman Numeral library
  2008-03-16  6:20 ` Nathan Mishra Linger
@ 2008-03-16 23:25   ` Ashish Agarwal
  0 siblings, 0 replies; 4+ messages in thread
From: Ashish Agarwal @ 2008-03-16 23:25 UTC (permalink / raw)
  To: caml-list; +Cc: Nathan Mishra Linger, Dave Benjamin

[-- Attachment #1: Type: text/plain, Size: 648 bytes --]

Thanks to both for the implementations. The algorithms differ, and so it was
not obvious that both generate equivalent results. I did some testing and
verified that they do (I ran for integers between 0 and 1000000).

Thanks again.


On Sun, Mar 16, 2008 at 2:20 AM, Nathan Mishra Linger <
nathan.mishralinger@gmail.com> wrote:
> Hi Ashish,
>
> Here is something I wrote a while back. Let me know if it works out for
you.
>
> Nathan
>
>
> 2008/3/14 Ashish Agarwal <agarwal1975@gmail.com>:
>
> >
> > Hi all. Does anyone have an OCaml libary for working with Roman
Numerals? Probably easy to write but am hoping it already exists. Thank you.
> >
> >

[-- Attachment #2: Type: text/html, Size: 874 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-03-16 23:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-14 16:50 is there a Roman Numeral library Ashish Agarwal
2008-03-14 20:29 ` [Caml-list] " Dave Benjamin
2008-03-16  6:20 ` Nathan Mishra Linger
2008-03-16 23:25   ` Ashish Agarwal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).