przemyxe0p
Scope of variable passed to function
Hi,
I am wondering why my solution wil not work
1. source: Perfect Numbers in Elixir on Exercism
2. solution attempt:
@spec classify(number :: integer) :: {:ok, atom} | {:error, String.t()}
def classify(number) do
case aliquot_sum(number) do
number -> ok(:perfect)
x when x > number -> ok(:abundant)
x when x < number -> ok(:deficient)
end
end
defp ok(item), do: {:ok, item}
defp aliquot_sum(number), do: get_divisors(number) |> Enum.sum()
defp get_divisors(number, divisor \\ 1, acc \\ [])
defp get_divisors(number, divisor, acc) when divisor > div(number,2), do: acc
defp get_divisors(number, divisor, acc) when rem(number, divisor) == 0,
do: get_divisors(number, divisor + 1, [divisor | acc])
defp get_divisors(number, divisor, acc), do: get_divisors(number, divisor + 1, acc)
end
3. question:
Complier is saying:
this clause cannot match because a previous clause at line 15
always matchesElixir
perfect_numbers.ex(15, 7): related
and
variable "number" is unused (there is a variable with the same name in the
context, use the pin operator (^) to match on it or prefix this variable
with underscore if it is not meant to be used)Elixir
Why is that, why it cannot match with number passed to function?
Most Liked
al2o3cr
This form rebinds the name number inside the case branch:
def some_fun(number) do
case 2*number do
number ->
# number here is bound to a new value, shadowing the argument named number
This will match ANY integer passed to some_fun.
ON THE OTHER HAND
Using the pin operator says “match this existing value”:
def some_fun(number) do
case 2*number do
^number ->
# number here is still bound to the argument
That will only match if some_fun is passed 0, since 2*0 == 0
2
krasenyp
kokolegorille
No, your logic is wrong and number might not be what You think…
As soon as there is a match, the rest of the case is discarded
the number in the case is not the number parameter, it is a new number
You probably should write this with cond
aliquot = aliquot_sum(number)
cond do
aliquot > number -> ...
aliquot < number -> ...
aliquot == number -> ...
end
1
Popular in Questions
Hello, how can I check the Phoenix version ?
Thanks !
New
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
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
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
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










