Charlists are created using the ~c
Sigil.
~c"hello"
Note that in older versions of Elixir, charlists are represented as 'hello'
with single quotes.
Although they look very similar to strings, the two data types are quite different from one another. A charlist is a list of integers. The integers represent the Unicode values of a given character — also known as code points.
[65, 66, 67]
# => ~c"ABC"
You can prepend a character with ?
to get its code point.
?A
# => 65
[?:, ?)]
# => ~c":)"
Because charlist are lists, you can work with them just like with any other list - using recursion and pattern matching.
[first_letter | _] = ~c"cat"
first_letter
# => 99
You can concatenate two lists using ++
.
~c"hi" ++ ~c"!"
# => ~c"hi!"
The longer the first list is, the slower the concatenation, so avoid repeatedly appending to lists of arbitrary length.
case
is a control flow structure that allows us to compare a given value against many patterns. Clauses in a case
expression are evaluated from top to bottom, until a match is found.
age = 15
case age do
0 -> ~c"infant"
age when age < 4 -> ~c"baby"
age when age < 13 -> ~c"child"
age when age < 18 -> ~c"teenager"
_ -> ~c"adult"
end
# => ~c"teenager"
You are working as a system administrator for a big company in Munich, Germany. One of your responsibilities is managing email accounts.
You have been hearing complaints from people saying they are unable to write emails to Mr. Müller. You quickly realize that most of the company uses an old email client that doesn't recognize müller@firma.de
as a valid email address because of the non-Latin character.
Telling people to give up their favorite old email client is a lost battle, so you decide to create sanitized aliases for all email accounts.
Implement the sanitize/1
function. It should accept a username as a charlist and return the username with all characters but lowercase letters removed.
Username.sanitize(~c"schmidt1985")
# => ~c"schmidt"
Extend the sanitize/1
function. It should not remove underscores from the username.
Username.sanitize(~c"mark_fischer$$$")
# => ~c"mark_fischer"
There are 4 non-Latin characters in the German alphabet, and all of them have commonly-recognized Latin substitutes.
German character | Latin substitute |
---|---|
ä | ae |
ö | oe |
ü | ue |
ß | ss |
Extend the sanitize/1
function. It should substitute German characters according to the table. You can safely assume all usernames are already downcase.
Username.sanitize(~c"cäcilie_weiß")
# => ~c"caecilie_weiss"
Sign up to Exercism to learn and master Elixir with 57 concepts, 159 exercises, and real human mentoring, all for free.