如何找到以给定前缀开头的字符串列表的字符串?
我想编写一个可以返回字符串列表的辅助函数
helper ['Excuse','Me', 'Exit', 'And'] 'Ex'
,这应该返回
['Excuse','Exit']
我搜索并在data.list
中找到isinfixof
函数可以比较两个字符串,但是我如何递归调用以找到正确的字符串列表
helper :: [String] -> String -> [String]
helper [] _ = []
helper (x:xs) std
| isInfixOf x std == True : helper
I want to write a helper function that could return a string list
helper ['Excuse','Me', 'Exit', 'And'] 'Ex'
and this should return
['Excuse','Exit']
I search up and find in Data.List
have the isInfixOf
function could compare two string, but how could I recursive call to find the correct String list
helper :: [String] -> String -> [String]
helper [] _ = []
helper (x:xs) std
| isInfixOf x std == True : helper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎完全在那里。
在列表上调用
helper
[“借口”,“我”,“退出”,“”和“] nath ,您要写:现在,当您将该列表与
x:xs
,xs
匹配时,这只是另一个列表。要调用助手
对此递归,您只需编写helper XS std
即可。您无需明确将
iSinfixof
的返回与true
进行比较。您还需要处理isinfixof x std
is true的情况。You're almost entirely there.
To call
helper
on a list["Excuse", "Me", "Exit", "And"]
named, oh let's saymyList
, you'd write:Now, when you pattern match that list with
x:xs
,xs
is the tail of the list, which is just another list. To callhelper
recursively on this, you'd just writehelper xs std
.You do not need to explicitly compare the return of
isInfixOf
toTrue
. You also need to handle the case whereisInfixOf x std
is not true.实际上,没有理由使用递归功能来做到这一点。您想保留满足特定谓词的列表的元素(
“ ex”
是元素的插图)。filt> filter
功能做到这一点。
例如:
There is not really a reason to do that with a recursive function. You want to keep elements of a list that fulfill a certain predicate (
"Ex"
being an infix of the element). Thefilter
function does exactly that.For example: