箭头只应出现在 case 表达式和匿名函数中
Arrow should only appear in cases expressions and anonymous functions
我正在尝试检查给定密钥是否存在于给定密钥的 dict
中。我对 Elm
和函数式编程比较陌生,所以我不确定我哪里出错了。
我收到的错误是:
An arrow should only appear in cases expressions and anonymous functions. Maybe
you want > or >= instead?
这是我返回 true
或 false
的尝试
dictExist : comparable -> Dict comparable v -> Bool
dictExist dict key =
Dict.get key dict
Just -> True
Maybe.Maybe -> False
另一方面,我也尝试了 Dict.member
,但也没有成功,并且假设我应该使用 Dict.member
而不是 Dict.get
...
你的代码有四个问题:
- 正如错误指出的那样,您在
case ... of
表达式外使用了箭头。
Maybe
类型的 Just
构造函数有一个伴随值,即字典中的项目,但您没有将它绑定到任何东西。您必须通过将其分配给通配符模式 _
. 来显式丢弃它
Maybe.Maybe
不是构造函数。这个应该是Nothing
,也就是Maybe
类型的另一个构造函数
- 您颠倒了
dictExist
的参数顺序
解决了这些问题后,这段代码应该可以工作了:
dictExist : comparable -> Dict comparable v -> Bool
dictExist key dict =
case Dict.get key dict of
Just _ -> True
Nothing -> False
但这实际上只是 Dict.member
的重新实现,其中有 the exact same type signature。因此,将 dictExist
的任何用法替换为 Dict.member
应该完全相同。
我正在尝试检查给定密钥是否存在于给定密钥的 dict
中。我对 Elm
和函数式编程比较陌生,所以我不确定我哪里出错了。
我收到的错误是:
An arrow should only appear in cases expressions and anonymous functions. Maybe you want > or >= instead?
这是我返回 true
或 false
dictExist : comparable -> Dict comparable v -> Bool
dictExist dict key =
Dict.get key dict
Just -> True
Maybe.Maybe -> False
另一方面,我也尝试了 Dict.member
,但也没有成功,并且假设我应该使用 Dict.member
而不是 Dict.get
...
你的代码有四个问题:
- 正如错误指出的那样,您在
case ... of
表达式外使用了箭头。 Maybe
类型的Just
构造函数有一个伴随值,即字典中的项目,但您没有将它绑定到任何东西。您必须通过将其分配给通配符模式_
. 来显式丢弃它
Maybe.Maybe
不是构造函数。这个应该是Nothing
,也就是Maybe
类型的另一个构造函数- 您颠倒了
dictExist
的参数顺序
解决了这些问题后,这段代码应该可以工作了:
dictExist : comparable -> Dict comparable v -> Bool
dictExist key dict =
case Dict.get key dict of
Just _ -> True
Nothing -> False
但这实际上只是 Dict.member
的重新实现,其中有 the exact same type signature。因此,将 dictExist
的任何用法替换为 Dict.member
应该完全相同。