neetspin
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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Sebb
You created a charlist, nothing wrong with that, it’s just a little odd.
LostKobrakai
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.
SaurabhSingh
@neetspin Here is another quick small function to do the same thing thing you’re looking for
stefanluptak
See more: