Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Integer to Roman numeral

Lean 4, 232 bytes def r(i):=Id.run do let M:=[999,899,499,399,99,89,49,39,9,8,4,3,0] let N:=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"] let mut s:="" let mut n:=i for x in Lis...

posted 1d ago by CrSb0001‭

Answer
#1: Initial revision by user avatar CrSb0001‭ · 2025-05-30T16:32:04Z (1 day ago)
# [Lean 4], 232 bytes

```lean
def r(i):=Id.run do
let M:=[999,899,499,399,99,89,49,39,9,8,4,3,0]
let N:=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
let mut s:=""
let mut n:=i
for x in List.range 13do while n>M[x]do s:=s++N[x];n:=n-M[x]-1
return s
```
Note that you will need to run this with `#eval!` instead of `#eval` since I never proved that the list indexes are valid (although this is entirely optional).

The reason I can't simply combine the two lists is because that is simply something that Lean doesn't allow. While I can combine different types using tuples, the problem there is that I can't index tuples.

---

[Try it online](https://live.lean-lang.org/#codez=CYUwZgBATgFAlgSgFwF4CSwB0UCuA7CYAewCgAbEAFwgFlUBtATmYBoAOVgFlYGZXWOLbiz4tG7ISJYAGALrkqEAHIMARDVUtVAYQ1aAIpp2Gt2owA0zWgDIXbW80bSOtANSfutaVfIrUAtjjUAM6oqqoKAUEQeKhwJGBEUBAAHhBwBNZwwZTYAIZ4AOYgEACMPMQQAO4AFnAUMQB8NPQpspWhKMEA1N1KrbIA3LEoeAC0LW1jpSRQVDhQBMEkJADEIABueWQAhNAQAGxHQA)

[Lean 4]:https://github.com/leanprover/lean4