【发布时间】:2017-05-29 14:18:14
【问题描述】:
我有一个类似List = ["google","facebook","instagram"] 的列表和一个字符串P1 = "https://www.google.co.in/webhp?pws=0&gl=us&gws_rd=cr"。
现在我需要找出List 的哪个元素存在于P1 中。
为此,我在递归函数下面实现,但它返回 ok 作为最终值,有没有办法在(在这种情况下)找到 google 时,然后返回 H 并终止其他递归调用在堆栈中。
我希望这个函数返回google。
traverse_list([],P1)-> ok;
traverse_list([H|T],P1) ->
Pos=string:str(P1,H),
if Pos > 1 ->
io:fwrite("Bool inside no match is ~p~n",[Pos]),
io:fwrite("inside bool nomathc, ~p~n",[H]),
H;
true->
io:fwrite("value found :: ~p~n",[Pos])
end,
traverse_list(T,P1).
【问题讨论】:
-
一个问题是io模块的输出函数如果成功则返回ok。您可以使用 io_lib 库再次尝试您的代码,看看会发生什么。