0

我只是在弄清楚控制流,这一切都非常奇怪和令人困惑,因为我以前从未使用过函数式语言,有人可以为我纠正这个问题:

-export([main/1, test/2]).

main([]) -> 
   if 
      test("blue yellow green", "yellow") == true ->
          {io:fwrite("found")};
      true ->
          {io:fwrite("not found")}
   end.


test(Source, Find) ->
    Pos = string:str(Source, Find),
    if
       Pos > 1 ->
            {true};  
       true ->
            {false}
    end. 
4

1 回答 1

3

更正后的版本:

-module(test).
-export([main/0, test/2]).

main() ->
    case test("blue yellow green", "yellow") of
        true -> io:fwrite("found~n");
        false -> io:fwrite("not found~n")
    end.

test(Source, Find) ->
    Pos = string:str(Source, Find),
    if
        Pos > 1 ->
            true;
        true ->
            false
    end.

当你只返回一个元素时,你不应该使用{and }

于 2013-01-22T10:48:24.533 回答