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

New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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