belgoros

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

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

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 :slight_smile:

sribe

sribe

But you’re still comparing lists of 1 character, and returning lists of 1 character, not single characters.

belgoros

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

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement