Haskell:测试列表是否包含特定的“子列表” [英] Haskell: test if list contains specific "sublist"
本文介绍了Haskell:测试列表是否包含特定的“子列表”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
xyz ::有一个技巧或前奏函数来测试列表是否包含特定的子字符串/子列表? [a] - > [a] - > Bool
xyzhello worldworl - >真
xyz [1,2,3,4,5,6,7,8,1,2,3,4,5] [7,8,1] - >真正的
我试图自己写一个,但这是一个微不足道的问题,我不希望重新发明轮子。 使用 isInfixOf
。
这些参数与您的要求相反 - 通常使用如下函数可读性强:worl`isInfixOf`hello world
(返回True)。
Is there a trick or a prelude function to test if a list contains a specific substring/sublist?
xyz :: [a] -> [a] -> Bool
xyz "hello world" "worl" -> True
xyz [1,2,3,4,5,6,7,8,1,2,3,4,5] [7,8,1] -> True
I tried to write one on my own, but this is a trivial problem and I don't want to reinvent the wheel.
解决方案
Use isInfixOf
from Data.List.
The parameters are the other way round from what you asked for --- it's usually most readable to use the function like this: "worl" `isInfixOf` "hello world"
(returns True).
这篇关于Haskell:测试列表是否包含特定的“子列表”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文