【问题标题】:lists:keyfind problems列表:keyfind问题
【发布时间】:2011-03-04 05:53:41
【问题描述】:

我这辈子都无法让 list:keyfind 在 Erlang 中像我期望的那样工作。

我有以下 eunit 测试:

should_find_key_test() ->
    NewList = lists:keystore("key", 1, [], {"key", "value"}),
    Value = case lists:keyfind("key", 1, NewList) of
        false ->
            notfound;
        {_key, _value} ->
            _value
    end,
    ?debugVal(Value).

每当我运行此测试时,我都会收到以下错误消息:

indextests:should_find_key_test (module 'indextests')...失败 ::错误:undef 在函数列表中:keyfind/3 称为 keyfind("key",1,[{"key","value"}]) 从 indextests 调用:should_find_key_test/0

谁能看出我做错了什么?

是不是说lists:keyfind 已经不存在了?

【问题讨论】:

  • 你运行的是哪个版本的 Erlang? lists:keyfind/3 是最新的,但我不记得它是在哪个版本中添加的。

标签: erlang


【解决方案1】:

lists:keyfind/3 是在 OTP/R13A 中引入的。我怀疑您使用的是旧版本。在 R13A 之前,您将使用 lists:serachkey/3。找到了相同的元组,但返回的数据结构略有不同。

should_find_key_test() ->
    NewList = lists:keystore("key", 1, [], {"key", "value"}),
    Value = case lists:keysearch("key", 1, NewList) of
        false ->
            notfound;
        {value {_Key, _Value}} ->
            _Value
    end,
    ?debugVal(Value).

release notes 显示在 STDLIB 1.6 版中添加的 keyfind/3 BIF。检查您的 STDLIB 版本。

【讨论】:

  • 确实是erlang版本。我阅读的安装说明建议下载 R12A 版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 2021-12-29
  • 2016-05-04
  • 2011-08-25
相关资源
最近更新 更多