kouluelixir

kouluelixir

Anonymous function arity error with is_string and is_integer

I am a beginner trying to make an anonymous function that takes two parameters. It needs to use guards to check if the two values are both strings or integers. If they both are strings, those strings need to be combined and printed. If they are both integers, they need to be added together and printed.

However, I am getting an error with different arities when using two is_bitstring and is_integer. Is the code below even possible using one anonymous function?

f = fn 

    x, y when is_bitstring(x) and is_bitstring(y) -> IO.puts x <> y

    x, y when is_integer(x) and is_integer(y) -> IO.puts x + y

    _ -> IO.puts "Both parameters are not strings"

end

Thank you!

Marked As Solved

kip

kip

ex_cldr Core Team

The clause _ -> IO.puts "Both parameters are not strings" is only matching on a single parameter when two are expected. Therefore more likely you should be using _, _ -> IO.puts "Both parameters are not strings".

BTW, if you are checking for strings, as in bytes, then is_binary/1 is the better guard to use. It won’t guarantee the parameter is a valid UTF8 string, but it will check that it is a a group of bytes. A bitstring is any number of bits.

Where Next?

Popular in Questions Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
ashish173
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

electic
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
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement