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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement