belgoros
Comparison of single-quoted and double-quotes Strings
Can anybody explain how the comparison of Strings and characters works Elixir for single-quoted and double-quoted characters (I’m using the term characters because as far I understand from “Programming Elixir-1.6” book:
When Elixir library documentation uses the word string (and most of the time it uses the word binary), it means double-quoted strings.
So, if I got it right, single-quoted strings are stored as char lists:
iex(3)> is_list 'AZERTY'
true
iex(4)> is_list 'A'
true
In this case, how can we compare a character (a letter) from the above example to match another character (letter), i.e., why can’t I just compare:
defp convert(letter) when 'C' == letter do
IO.puts("+++++++ equal!!! ++++++++++ ")
end
However, it works in iex session:
iex(5)> letter = 'C'
'C'
iex(6)> 'C' == letter
true
What am I missing ?
Thank you
Marked As Solved
Virviil
Character data type is absolutely the same as Unsigned Integer type for most programming languages.
The same is for Elixir.
So, 'AZERTY' is list of integers, that are just looks like letters (or string). For example, you can add one more element to the list like this:
iex> 'AZERY' ++ [0]
[65, 90, 69, 82, 89, 0]
And as expected you see the list of integer (because Elixir cant convert 0 to a letter)
As you see here, A = 65. So, what is 'A'? OF cource it’s list with one element
iex> 'A' == [65]
true
Now, the question is:
How can we represent char, if we don’t want to look at the ASCII table every time, to see that A is 65?
Using ? syntax sugar!
iex> ?A
65
Now, you can do:
defp convert(letter) when ?C == letter do
IO.puts("+++++++ equal!!! ++++++++++ ")
end
Also Liked
Virviil
Enum.each returns :ok, you need Enum.map here.
It’s good idea to ask these questions from your Exercism mentor, not on forum ![]()
sribe
But you’re still comparing lists of 1 character, and returning lists of 1 character, not single characters.
belgoros
The complete module:
@spec to_rna([char]) :: [char]
def to_rna(dna) do
Enum.map(dna, &convert(&1))
end
defp convert(?G) do
?C
end
defp convert(?C) do
?G
end
defp convert(?T) do
?A
end
defp convert(?A) do
?U
end
defp convert(letter) do
IO.puts(:stderr, "Couldn't find matching for #{letter}")
letter
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









