erlang lists:keyfind的一个小坑

来源:互联网 发布:温庭筠入骨相思知不知 编辑:程序博客网 时间:2024/05/21 22:42
Key = 2/2,FindReuslt = lists:keyfind(Key, 1, [{1, 1}],case FindReuslt of  {Key, Value} ->         find;   false ->         not_findend.

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

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


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

这里回到基础概念
erlang 中
== 是equal                     1.0 == 1  true
=:= 是 exactly equal        1.0 =:= 1 false
所以lists:keyfind时会查找到结果,但是在匹配时就找不到对应项。
0 0
原创粉丝点击