Strings and Runes in Idris
Idris, like many functional programming languages, treats strings as a sequence of characters rather than bytes. However, we can still explore similar concepts to demonstrate string handling and character manipulation in Idris.
In this Idris version:
We use
Data.String
andData.Char
for string and character operations.The
s
constant is defined as a string containing Thai characters.We use
length s
to get the number of characters in the string. In Idris, this gives us the character count, not the byte count.We use
unpack s
to convert the string to a list of characters, which we can then manipulate.To iterate over characters with their indices, we use
zip
with a range and the unpacked string.The
examineChar
function demonstrates character comparison. In Idris, character literals are enclosed in single quotes.We use
traverse_
for side-effecting operations over lists, which is similar to afor
loop in imperative languages.
To run this program, save it as StringsAndChars.idr
and use the Idris compiler:
This example demonstrates how Idris handles strings and characters. Unlike some languages, Idris treats strings as sequences of Unicode characters by default, simplifying operations on text containing non-ASCII characters.