Zulip Chat Archive
Stream: general
Topic: String.reverse
Maris Ozols (Dec 28 2025 at 18:19):
Not sure if I'm posting this in the right channel...
The last example in List.map and List.mapTr use String.reverse but the latest version of Lean (lean4:v4.26.0) doesn't seem to have such function. I ended up implementing it myself using List.reverse:
def reverse (s : String) : String :=
String.ofList s.toList.reverse
Henrik Böving (Dec 28 2025 at 18:38):
You could potentially use String.revChars instead.
Alfredo Moreira-Rosa (Dec 28 2025 at 19:04):
revChars and List.reverse seems to break uft8 composed emojis :
#eval String.ofList "1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣0️⃣".revChars.toList -- "⃣️0⃣️7⃣️6⃣️5⃣️4⃣️3⃣️2⃣️1"
#eval String.ofList "1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣0️⃣".toList.reverse -- "⃣️0⃣️7⃣️6⃣️5⃣️4⃣️3⃣️2⃣️1"
does lean support graphemes splitting ?
Here a working js solution :
function reverseString(str) {
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
return [...segmenter.segment(str)]
.map(s => s.segment)
.reverse()
.join('')
}
reverseString("1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣0️⃣") // '0️⃣7️⃣6️⃣5️⃣4️⃣3️⃣2️⃣1️⃣'
Is this also possible with Lean API ?
Weiyi Wang (Dec 28 2025 at 19:10):
This kind of unicode problem is always a nightmare in any programming language. It boils down to one question "what is a character"
Alfredo Moreira-Rosa (Dec 28 2025 at 19:16):
Maybe it's just an API issue and behind the scene Lean understand unicode graphemes (composition of many code points that represent a single visual element)?
Henrik Böving (Dec 28 2025 at 19:17):
No, Lean, just like Rust's stdlib, decided to not support graphemes, the unit of character is a unicode scalar value.
Alfredo Moreira-Rosa (Dec 28 2025 at 19:24):
Thanks for the insight. Seems like a fun small project for a reservoir library :)
Last updated: Feb 28 2026 at 14:05 UTC