Charlists are created using single quotes.
Although they look 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] # => 'ABC'
You can prepend a character with
? to get its code point.
?A # => 65 [?:, ?)] # => ':)'
Because charlist are lists, you can work with them just like with any other list - using recursion and pattern matching.
[first_letter | _] = 'cat' first_letter # => 99
You can concatenate two lists using
'hi' ++ '!' # => '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 -> 'infant' age when age < 4 -> 'baby' age when age < 13 -> 'child' age when age < 18 -> 'teenager' _ -> 'adult' end # => '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.
sanitize/1 function. It should accept a username as a charlist and return the username with all characters but lowercase letters removed.
Username.sanitize('schmidt1985') # => 'schmidt'
sanitize/1 function. It should not remove underscores from the username.
Username.sanitize('mark_fischer$$$') # => '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|
sanitize/1 function. It should substitute German characters according to the table. You can safely assume all usernames are already downcase.
Username.sanitize('cäcilie_weiß') # => 'caecilie_weiss'
Sign up to Exercism to learn and master Elixir with 57 concepts, 157 exercises, and real human mentoring, all for free.