Strings and Runes in Erlang
In Erlang, strings are represented as lists of integers, where each integer corresponds to a Unicode code point. This is different from many other languages where strings are often treated as a separate data type.
In this Erlang version:
We define a module
strings_and_runes
with amain/0
function.Strings in Erlang are lists of integers, where each integer represents a Unicode code point.
We use
length/1
to get the number of characters in the string, which is equivalent to both the byte length and the rune count in this case.To print the hexadecimal representation of each character, we use a list comprehension with
io:format/2
.We use
lists:foldl/3
to iterate over the string, printing each character’s Unicode code point and its position.Instead of
DecodeRuneInString
, we uselists:nth/2
to access individual characters in the string.The
examine_char/1
function demonstrates pattern matching on specific characters.
To run this program:
Note that Erlang handles Unicode strings natively, so we don’t need to use any special libraries for UTF-8 decoding. The concept of “runes” doesn’t exist in Erlang, as each element in a string is already a full Unicode code point.