Erlang:右侧值不匹配

Erlang:右侧值不匹配,erlang,pattern-matching,Erlang,Pattern Matching,Erlang程序中的常见错误消息如下: ** exception error: no match of right hand side value 'foo' in function module:function/2 (file.erl, line 42) 如何调试此错误?以下是如何调试此类错误: 转到模块:函数/2(file.erl,第42行) 找到确实存在的有问题的匹配操作 将左侧更换为新的变量。在这里,您可能会发现您正在尝试对一个已经绑定的变量进行模式匹配 使用新变量添加对的

Erlang程序中的常见错误消息如下:

** exception error: no match of right hand side value 'foo'
     in function module:function/2 (file.erl, line 42)

如何调试此错误?

以下是如何调试此类错误:

  • 转到
    模块:函数/2(file.erl,第42行)

  • 找到确实存在的有问题的匹配操作

  • 将左侧更换为新的变量。在这里,您可能会发现您正在尝试对一个已经绑定的变量进行模式匹配

  • 使用新变量添加对的调用

  • 再次运行该程序以打印此变量的值,并了解其与给定模式不匹配的原因

  • 以下是一些例子:

    • :

      将其替换为:

      Fresh1 = io:get_line("Do you want to chat?"),
      erlang:display(Fresh1),
      {_, Input} = Fresh1
      
      再次运行程序:

      1> module:run().
      Do you want to chat? Yes
      "Yes\n"
      ** exception error: no match of right hand side value "Yes\n"
        in function module:function/2 (file.erl, line 44)
      
      您可以看到,
      io:get\u line/1
      返回一个字符串而不是元组,因此对
      {uu,Input}
      的匹配失败

    • :

      在Erlang shell中:

      2> Pid = echo:start().
      ** exception error: no match of right hand side value <0.41.0>
      

    错误报告比简单的错误匹配更友好,它还提供了无法匹配的值。如果该值足够小,则会完全显示该值,并且通常足以了解发生了什么问题:

    -module(err).
    
    -export([test/0]).
    
    test() ->
        "ok\n" = io:get_line("an input ? ").
    
    test2() ->
        F = fun() ->
            "ok\n" = io:get_line("an input ? ")
        end,
        F().
    
    在外壳中:

    1> c(err).
    {ok,err}
    2> err:test().
    an input ? ok
    "ok\n"
    3> err:test().
    an input ? ko
    ** exception error: no match of right hand side value "ko\n"
         in function  err:test/0 (err.erl, line 6)
    4> F = fun() -> a = 10 end.
    #Fun<erl_eval.20.106461118>
    5> F().
    ** exception error: no match of right hand side value 10
    6> err:test2().
    an input ? ok
    "ok\n"
    7> err:test2().
    an input ? ko
    ** exception error: no match of right hand side value "ko\n"
         in function  err:'-test2/0-fun-0-'/0 (err.erl, line 10)
    8>  
    
    1>c(错误)。
    {好的,呃}
    2> 错误:test()。
    输入?好啊
    “确定\n”
    3> 错误:test()。
    输入?让开
    **异常错误:右侧值“ko”不匹配\n
    函数内错误:测试/0(err.erl,第6行)
    4> F=fun()->a=10结束。
    #乐趣
    5> F()。
    **异常错误:右侧值10不匹配
    6> 错误:test2()。
    输入?好啊
    “确定\n”
    7> 错误:test2()。
    输入?让开
    **异常错误:右侧值“ko”不匹配\n
    在函数err:'-test2/0-fun-0-'/0中(err.erl,第10行)
    8>  
    
    4> f(Pid).
    ok
    5> Pid.
    * 1: variable 'Pid' is unbound
    6> Pid = echo:start().
    <0.49.0>
    
    -module(err).
    
    -export([test/0]).
    
    test() ->
        "ok\n" = io:get_line("an input ? ").
    
    test2() ->
        F = fun() ->
            "ok\n" = io:get_line("an input ? ")
        end,
        F().
    
    1> c(err).
    {ok,err}
    2> err:test().
    an input ? ok
    "ok\n"
    3> err:test().
    an input ? ko
    ** exception error: no match of right hand side value "ko\n"
         in function  err:test/0 (err.erl, line 6)
    4> F = fun() -> a = 10 end.
    #Fun<erl_eval.20.106461118>
    5> F().
    ** exception error: no match of right hand side value 10
    6> err:test2().
    an input ? ok
    "ok\n"
    7> err:test2().
    an input ? ko
    ** exception error: no match of right hand side value "ko\n"
         in function  err:'-test2/0-fun-0-'/0 (err.erl, line 10)
    8>