belgoros

belgoros

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

Showing Posts 1 to 10

NobbZ

NobbZ

The problem with your compare function is, that it will crash as soon as you pass something that is not 'C'.

It should work besides of that. What is it you see instead?

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
belgoros

belgoros OP

I’m trying to catch the letters as follows:

def to_rna(dna) do
    Enum.each(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("Unknown letter")
  end

And the test:

assert RNATranscription.to_rna('G') == 'C'

fails as follows:

Assertion with == failed
     code:  assert RNATranscription.to_rna('G') == 'C'
     left:  :ok
     right: 'C'
     stacktrace:
       rna_transcription_test.exs:13: (test)

Can’t understand what’s really wrong :frowning:

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:

peerreynders

peerreynders

'C' == letter

is simply an expression the either evaluates to true or false

when 'C' == letter

Here that expression is part of a guard, i.e. if

  • true → match
  • false → match fails
def convert(letter) when 'C' == letter do
    IO.puts("+++++++ equal!!! ++++++++++ ")
end

Is equivalent to

def convert('C') do
  IO.puts("+++++++ equal!!! ++++++++++ ")
end

Given the absence of other function clauses a failed match will result in a program crash. To avoid that:

def convert('C') do
  IO.puts("+++++++ equal!!! ++++++++++ ")
end
def convert(_) do
  IO.puts("+++++++ not equal!!! ++++++++++ ")
end

This function convert/1 is one function with two function clauses (function heads).

belgoros

belgoros OP

OK, it is getting clearer now. Thank you very much for such a detailed explanation, really useful :smile:
Taking into account that Enum.each returns just :ok and map returns a list, I think a better solution would be to return a corresponding matching letter, so in pseudo language it would read:

if 'C' => return 'G'
if 'G' => return 'C'
etc...
peerreynders

peerreynders

Look at case do

Also remember that everything is an expression (there are no statements) so everything evaluates to some kind of value.

This is why there is no return statement - functions have to evaluate to a value just like expressions, so they automatically “return” the last value they evaluated.

sribe

sribe

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

belgoros

belgoros OP

As I’m getting a list of characters (either of just one, A, or a sequence like AZERTY), I think I have to

  • make them match one by one to make a translation to other characters according to the translation table (like A should be replaced with T, Z → with U, etc.)
  • build another chain of translated characters and return it.
belgoros

belgoros OP

Yeah, it is like in Ruby methods where the last evaluated expression is returned from a method.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews