neetspin

neetspin

Help Understanding Return of Function

Hello,

I’m going through Elixir in Action right now and one of the exercises is to create a function that returns the list of all values between two numbers. When I use certain value pairs it seems like I’m getting returned bitstrings for some reason. The output should just be one, and with other values it returns a single value list with 1 but there’s two cases I’ve ran into so far where it’s not returning properly.

Here’s the code:

defmodule NumUtil do
  def get_range(num1, num2) do
    do_range(Kernel.min(num1, num2), Kernel.max(num1, num2), 1, [])
  end
  
  defp do_range(start_num, target_num, current_iter, list) when start_num + current_iter == target_num do
    list
  end

  defp do_range(start_num, target_num, current_iter, list) do
    update_list = [start_num + current_iter | list ]
    new_iter = current_iter + 1
    do_range(start_num, target_num, new_iter, update_list)
  end
end

So when I do NumUtil.get_range(12, 15) I get back the expected [14, 13] but when I do NumUtil.get_range(12, 14) I get back '\r'. I had initial input at 0 instead of one before and with 12,13 it returned back '\f'. I figure it was something I didn’t understand about single element lists but if I do it on a different value pair like (1,3) I get back the expected [2]… There are other pairs that seem to return strings but I’m not sure why. Could anyone please help me understand the behavior?

Also what is the “proper” way to write this function, I’m pretty sure mines a bit of a mess so any tips would be appreciated. Thanks for the help in advance!

Most Liked

stefanluptak

stefanluptak

See more: :slightly_smiling_face:

Sebb

Sebb

You created a charlist, nothing wrong with that, it’s just a little odd.

iex(19)> inspect 'hello', charlists: :as_lists 
"[104, 101, 108, 108, 111]"
iex(20)> inspect 'hello'                      
"'hello'"

iex(21)> inspect [104, 101, 108, 108, 111]
"'hello'"
iex(22)> inspect [104, 101, 108, 108, 111], charlists: :as_lists
"[104, 101, 108, 108, 111]"

iex(24)> [104, 101, 108, 108, 111] == 'hello'
true
iex(25)> [104, 101, 108, 108, 111] == "hello"
false
iex(26)> 'hello' == "hello"
false

iex(27)> is_list('hello')
true
iex(28)> is_list("hello")
false
iex(29)> is_binary("hello")
true
SaurabhSingh

SaurabhSingh

In elixir by default iex print their ascii value if a valid value is input for example
iex(12)> [101, 102, 103, 104]
‘efgh’

Any number which is not valid will be print as is as List format for example below
iex(13)> [411, 412, 413, 414, 415]
[411, 412, 413, 414, 415]

Now if want to print the actual numbers instead of ascii then we need to try below

iex(14)> [101, 102, 103, 104] |> inspect(charlists: :as_lists)
“[101, 102, 103, 104]”

Refer this link for more details.

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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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

Other popular topics Top

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
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
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
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement