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

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
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
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement