Notes and exercises while reading through Programming Elixir by Dave Thomas.
Continuing down the road of "Part I - Conventional Programming", this chapter covers strings, character lists and binaries.
A non-conventional thing about Elixir strings is that double-quoted strings are actual "strings", while single-quoted list of characters are character lists. This is important, especially when sending params to functions that expect one or the other. Both string and character list forms support interpolation, heredocs and sigils.
iex(3)> name = "Dave"
"Dave"
iex(4)> "Hello, #{name}"
"Hello, Dave"
iex(5)> name = 'Don'
'Don'
iex(6)> "Hello, #{name}"
"Hello, Don"
- ''' or """
- and indent
- good for docs
IO.puts "start"
IO.write """
my
string
"""
IO.puts "end"
Like Ruby, Elixir has an alternative syntax for some literals. starts with a tilde, followed by an upper- or lowercase letter, some delimited content...
iex(12)> ~W[the way to win is to play a lot]a
[:the, :way, :to, :win, :is, :to, :play, :a, :lot]
iex(13)> ~w[the way to win is to play a lot]c
['the', 'way', 'to', 'win', 'is', 'to', 'play', 'a', 'lot']
iex(14)> ~w[the way to win is to play a lot]s
["the", "way", "to", "win", "is", "to", "play", "a", "lot"]
- Single quoted, e.g.
charcode
. - Is a list of integer character codes.
iex(15)> 'pole' ++ 'vault'
'polevault'
iex(16)> 'pole' -- 'vault'
'poe'
See mylist.exs for my solutions.
iex(13)> MyString.anagram?('mom', 'omm')
true
iex(14)> MyString.anagram?('mom', 'ohm')
false
- a sequence of bits
<< bit1, bit2, ... >>
- strings are binaries
- so need to use
String
module instead of lists
iex(1)> dqs = "∂x/∂y"
"∂x/∂y"
iex(2)> String.length dqs
5
iex(3)> byte_size dqs
9
iex(4)> String.at(dqs, 0)
"∂"
iex(5)> String.split(dqs, "/")
["∂x", "∂y"]
- can match on type
binary, bits, bitstring, bytes, float, integer, utf8, utf16, and utf32
Processing binaries:
- use
<< head::utf8, tail::binary >>
as params in recursive function - look for empty binary
<<>>
as terminating pattern match
See mystring.exs.