I am trying to check if a given key exist in a dict
given the key. I am relatively new to Elm
and functional programming, so I am not sure where I am going wrong.
The error I am receiving is:
An arrow should only appear in cases expressions and anonymous functions. Maybe you want > or >= instead?
Here's my attempt at returning true
or false
dictExist : comparable -> Dict comparable v -> Bool
dictExist dict key =
Dict.get key dict
Just -> True
Maybe.Maybe -> False
On another note I have tried the Dict.member
as well, but no success either and would assume instead of the Dict.get
I should be using Dict.member
for this...
There are four problems with your code:
case ... of
expression.Maybe
type's Just
constructor has an accompanying value, the item from the dict, but you don't bind it to anything. You have to explicitly discard it by assigning it to the wildcard pattern, _
.Maybe.Maybe
is not a constructor. This should be Nothing
, which is the other constructor of the Maybe
type.dictExist
Having fixed these problems, this code should work:
dictExist : comparable -> Dict comparable v -> Bool
dictExist key dict =
case Dict.get key dict of
Just _ -> True
Nothing -> False
But this is really just a reimplmentation of Dict.member
, which has the exact same type signature. Replacing any usage of dictExist
with Dict.member
should therefore work exactly the same.