2013-01-22 133 views
0

我只是找出控制流,它的一切非常奇怪和困惑,因为我以前从未使用过函数式语言,可能会有人纠正这对我来说:返回true或false发现串

-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. 

回答

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. 

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

+0

我可能会使用'case string:str(Source,Find)of P when P> = 1 - > true; 0 - > false end'或类似的东西 –

+1

甚至只是'string:str(Source,Find)> = 1.' – rvirding