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

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
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...
3271 126479 1222
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

We're in Beta

About us Mission Statement