Roman Numerals and Haskell Syntax abuse

The following declaration for a function for converting positive integers to Roman numerals is 181 characters long. Is there a shorter one? roman=f[(i-j,i)|i<-[2,4..8],j<-[2,1]]where f(d@((a,b):c))n|n<1=""|n>=t!!a=s!!a:f d(n-t!!a)|1>0=if n+t!!b>=t!!a then s!!b:f d(n+t!!b)else f c n;s="mdclxvi";t=[1000,500,100,50,10,5,1]

G'day all.
Quoting George Russell
The following declaration for a function for converting positive integers to Roman numerals is 181 characters long.
Is there a shorter one?
Being a judge, I can't write your obfuscated haskell contest entry for you. However, as a suggestion, you may be able to merge the if-then-else with the guard syntax. That should save a few characters.
roman=f[(i-j,i)|i<-[2,4..8],j<-[2,1]]where f(d@((a,b):c))n|n<1=""|n>=t!!a=s!!a:f d(n-t!!a)|1>0=if n+t!!b>=t!!a then s!!b:f d(n+t!!b)else f c n;s="mdclxvi";t=[1000,500,100,50,10,5,1]
Cheers, Andrew Bromage

ajb:
Quoting George Russell
: The following declaration for a function for converting positive integers to Roman numerals is 181 characters long.
Is there a shorter one?
Being a judge, I can't write your obfuscated haskell contest entry for you. However, as a suggestion, you may be able to merge the if-then-else with the guard syntax. That should save a few characters.
roman=f[(i-j,i)|i<-[2,4..8],j<-[2,1]]where f(d@((a,b):c))n|n<1=""|n>=t!!a=s!!a:f d(n-t!!a)|1>0=if n+t!!b>=t!!a then s!!b:f d(n+t!!b)else f c n;s="mdclxvi";t=[1000,500,100,50,10,5,1]
I feel that obfuscated code should always have no space characters, and line-justified such that each line has the same width, and have the minimal number of keywords possible. And who uses alpha-numerics for varsyms anyway -- throw them out! All this is easy to achieve by turning off layout syntax, using extra parens, and rewriting keywords into lambda abstractions. At some point one loses the goal of writing the least number of characters, and instead looks for the most unreadable code. The point at which this happens should have a name.. Perhaps "the obfuscation horizon" - the point from which no code returns? Witness http://www.cse.unsw.edu.au/~dons/pretty.html as an example of this syndrome. -- Don

George Russell wrote:
The following declaration for a function for converting positive integers to Roman numerals is 181 characters long.
Is there a shorter one?
Yes, thanks to various contributors to this list, the shortest is now 148 characters!! roman=f 0;f a n|n<1=""|n>=t!!a=s!!a:f a(n-t!!a)|n+t!!b>=t!!a=s!!b:f a(n+t!!b)|1>0=f(a+1)n where b=2*div a 2+2;s="mdclxvi";t=[1000,500,100,50,10,5,1]

George Russell wrote (snipped):
Now 145. roman=(0#);a#n|n<1=""|n>=t!!a=s!!a:a#(n-t!!a)|n+t!!b>=t!!a=s!!b:a#(n+t!!b)|1>0=(a+1)#n where b=2*div a 2+2;s="mdclxvi";t=[1000,500,100,50,10,5,1]
138 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|n+c>=t=s!!(2*e):(n+c)!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e;t=10^d*(1+4*m)

Christian Maeder wrote:
George Russell wrote (snipped):
Now 145. roman=(0#);a#n|n<1=""|n>=t!!a=s!!a:a#(n-t!!a)|n+t!!b>=t!!a=s!!b:a#(n+t!!b)|1>0=(a+1)#n where b=2*div a 2+2;s="mdclxvi";t=[1000,500,100,50,10,5,1]
138 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|n+c>=t=s!!(2*e):(n+c)!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e;t=10^d*(1+4*m)
now 134 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|c>=t=s!!(2*e):c!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e+n;t=10^d*(1+4*m)

At 11:44 06/07/04 +0200, Christian Maeder wrote:
Christian Maeder wrote:
George Russell wrote (snipped):
Now 145. roman=(0#);a#n|n<1=""|n>=t!!a=s!!a:a#(n-t!!a)|n+t!!b>=t!!a=s!!b:a#(n+t!!b)|1>0=(a+1)#n where b=2*div a 2+2;s="mdclxvi";t=[1000,500,100,50,10,5,1]
138 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|n+c>=t=s!!(2*e):(n+c)!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e;t=10^d*(1+4*m)
now 134 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|c>=t=s!!(2*e):c!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e+n;t=10^d*(1+4*m)
Doesn't that need "import Array"? #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

Graham Klyne wrote:
now 134 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|c>=t=s!!(2*e):c!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e+n;t=10^d*(1+4*m)
Doesn't that need "import Array"?
No, "!" is user defined here (since ghc does not like "#" as infix) btw, now 127: roman=(!5);n!a|n<1=""|n>=t=s!!(a+1):(n-t)!a|c>=t=s!!(2*d):c!a|1>0=n!(a-1)where d=a`div`2;s="ivxlcdm";c=10^d+n;t=5^(d+1)*2^(a-d) Christian

now 134 roman=(!6);n!a|n<1=""|n>=t=s!!a:(n-t)!a|c>=t=s!!(2*e):c!a|1>0=n!(a-1)where(d,m)=a`divMod`2;e=d+m-1;s="ivxlcdm";c=10^e+n;t=10^d*(1+4*m)
Gosh! Anyway, you missed the roman symbols for 5000 (U+2181) and 10000 (U+2182)... ;-) The ones for 50000 and 100000 aren't in Unicode yet, nor is the "canopy" used to write even larger values (see http://std.dkuug.dk/jtc1/sc2/wg2/docs/n2738). /Kent K
participants (6)
-
ajb@spamcop.net
-
Christian Maeder
-
dons@cse.unsw.edu.au
-
George Russell
-
Graham Klyne
-
Kent Karlsson