Created
July 10, 2015 20:49
-
-
Save mndrix/0b14fcd4a8ab873b2016 to your computer and use it in GitHub Desktop.
Roman numerals in Prolog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- use_module(library(clpfd)). | |
dcg(Arabic) --> | |
{ Arabic in 1..3999 }, | |
roman(Arabic), | |
!. | |
roman(Total) --> | |
{ Rest #>= 0 }, | |
{ Total #= Value + Rest }, | |
digit(Value), | |
roman(Rest). | |
roman(0) --> "". | |
digit(1000) --> "M". | |
digit(900) --> "CM". | |
digit(500) --> "D". | |
digit(400) --> "CD". | |
digit(100) --> "C". | |
digit(90) --> "XC". | |
digit(50) --> "L". | |
digit(40) --> "XL". | |
digit(10) --> "X". | |
digit(9) --> "IX". | |
digit(5) --> "V". | |
digit(4) --> "IV". | |
digit(1) --> "I". |
Thank you for sharing! Example with the most general query:
?- phrase(dcg(N), Rs).
N = 3999,
Rs = [77, 77, 77, 67, 77, 88, 67, 73, 88].
suggesting that this is the only solution, which it of course isn't. For logically sound behaviour, you can throw an instantiation error
if the arguments are not sufficiently instantiated. Regarding naming, consider using arabic_roman//1
instead of dcg//1
. Very nice idea, thank you again!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example,