Haskell 布尔模式子列表

Haskell 布尔模式子列表,haskell,Haskell,我需要从列表中提取奇数位置的元素。在Data.List库中,我找到了有关的任何信息。因此,我创建了以下函数。我想知道是否有一个库包含这个函数和其他类似的函数,是否有可能对我的函数进行显著的重构。谢谢 extractByPattern p l = extractByPatternRaw bp l where bp = map (== 't') p extractByPatternRaw p l = foldr select [] coupledList where couple

我需要从列表中提取奇数位置的元素。在Data.List库中,我找到了有关的任何信息。因此,我创建了以下函数。我想知道是否有一个库包含这个函数和其他类似的函数,是否有可能对我的函数进行显著的重构。谢谢

extractByPattern p l = extractByPatternRaw bp l
  where
  bp = map (== 't') p

extractByPatternRaw p l = foldr  select [] coupledList
  where
  coupledList = zip (concat . repeat $ p) l
  select (b,x) acc
   | b         = x : acc
   | otherwise = acc

oddPos = extractByPattern "tf"
-- ex. oddPos [1..20] == [1,3,5,7,9,11,13,15,17,19]

everyTwoAndFivePos = extractByPattern "ftfft"
-- ex. everyTwoAndFivePos [1..20] == [2,5,7,10,12,15,17,20]
作为替代方案:

λ map fst $ filter snd $ zip [1..20] $ cycle . map (== 't') $ "ftfft"
[2,5,7,10,12,15,17,20]
因此,您可以执行以下操作:

extractByPattern pattern list = map fst $ filter snd $ zip list $ cycle . map (== 't') $ pattern


Hoogle中没有跳出或,这将保存
zip
-
过滤器
-
snd
-
map
-
fst
hoop-jumping.

至少在我看来,这是列表理解更容易阅读的情况之一:
extractbyptern-pattern-list=[i |(i,t)@Zeta您的解决方案非常简单elegant@Zeta:
[i |(i,'t')@AndrásKovács它甚至更好!