erlang lists:keyfind的一个小坑

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 19:39   44   0
Key = 2/2,
FindReuslt = lists:keyfind(Key, 1, [{1, 1}],
case FindReuslt of
  {Key, Value} ->
         find;
   false ->
         not_find
end.

这段代码会报没有匹配的错误

原因是 Key = 2/2 其实 Key被赋值为1.0 (摔,这个基础概念都能忘记)


lists:keyfind 文档中给出的匹配规则是 Searches the list of tuples TupleList for a tuple whoseNth element compares equal toKey

这里回到基础概念
erlang 中
== 是equal 1.0 == 1 true
=:= 是 exactly equal 1.0 =:= 1 false
所以lists:keyfind时会查找到结果,但是在匹配时就找不到对应项。
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP