compiler-errors - 模式匹配Erlang?

我有一个带有if语句的代码,该语句试图让用户输入yes或no,如果用户输入的不是yes,则请求被拒绝。这是我得到的错误:

** exception error: no match of right hand side value "yes\n"
     in function  messenger:requestChat/1 (messenger.erl, line 80)

代码在这里:
requestChat(ToName) ->
    case whereis(mess_client) of
        undefined ->
            not_logged_on;
         _ -> mess_client ! {request_to, ToName},
                request_to = io:get_line("Do you want to chat?"),
                {_, Input} = request_to,
                if(Input == yes) ->
                    ok;
                    true -> {error, does_not_want_to_chat}
                end
    end.

最佳答案

在这种情况下,您可以使用 shell 程序测试为什么会出错(或转到文档)。

如果你试试:

1> io:get_line("test ").
test yes
"yes\n"
2>

您会看到io:get_line/1不会返回元组{ok,Input},而是返回以回车符结尾的简单字符串"yes\n"。这就是错误消息中报告的内容。

因此,您的代码可以修改为:
requestChat(ToName) ->
    case whereis(mess_client) of
        undefined ->
            not_logged_on;
         _ -> mess_client ! {request_to, ToName},
                if 
                    io:get_line("Do you want to chat?") == "yes\n" -> ok;
                    true -> {error, does_not_want_to_chat}
                end
    end.

但我更喜欢案例陈述
    requestChat(ToName) ->
        case whereis(mess_client) of
            undefined ->
                not_logged_on;
             _ -> mess_client ! {request_to, ToName},
                    case io:get_line("Do you want to chat?") of
                        "yes\n" -> ok;
                        "Yes\n" -> ok;
                        _ -> {error, does_not_want_to_chat}
                    end
        end.

https://stackoverflow.com/questions/23575901/

相关文章:

haskell - 尝试遍历森林 Haskell 编译错误

oop - 对字段和方法使用私有(private)而不是 protected 原因

compiler-errors - 编程新手,无法让Visual Studio 2013打开包含文件

c# - C#asp.net中UPDATE语句中的语法错误

c# - 为什么SubscribeOn在这里不能作为扩展方法而是直接调用?

c# - C#简单兴趣计算器切换错误

compiler-errors - 编译器无法识别显式类型

android - Android : Take numbers, do calculations,

vba - VBA中的子级

haskell - 如何编写map语句来比较haskell中两个列表的每个元素?