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
Eiji

Eiji

https://hexdocs.pm/elixir/master/syntax-reference.html#integers-in-other-bases-and-unicode-code-points

IVR

IVR

I’m a little late to the party, but I just wanted to share this video which I found useful to understand what’s going on here.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement