在Haskell中使用填充进行压缩

时间:2010-06-10 15:40:28

标签: haskell functional-programming

有几次我发现自己想要在Haskell中使用zip,将填充添加到较短的列表而不是截断较长的列表。这很容易写。 (Monoid在这里适合我,但你也可以传入你想要用于填充的元素。)

zipPad :: (Monoid a, Monoid b) => [a] -> [b] -> [(a, b)]
zipPad xs [] = zip xs (repeat mempty)
zipPad [] ys = zip (repeat mempty) ys
zipPad (x:xs) (y:ys) = (x, y) : zipPad xs ys

尝试定义zipPad3时,这种方法很难看。我键入了以下内容然后意识到它当然不起作用:

zipPad3 :: (Monoid a, Monoid b, Monoid c) => [a] -> [b] -> [c] -> [(a, b, c)]
zipPad3 xs [] [] = zip3 xs (repeat mempty) (repeat mempty)
zipPad3 [] ys [] = zip3 (repeat mempty) ys (repeat mempty)
zipPad3 [] [] zs = zip3 (repeat mempty) (repeat mempty) zs
zipPad3 xs ys [] = zip3 xs ys (repeat mempty)
zipPad3 xs [] zs = zip3 xs (repeat mempty) zs
zipPad3 [] ys zs = zip3 (repeat mempty) ys zs
zipPad3 (x:xs) (y:ys) (z:zs) = (x, y, z) : zipPad3 xs ys zs

此时我作弊,只是使用length选择最长的列表并填充其他列表。

我是否忽略了一种更优雅的方式来实现这一目标,还是已经在某处定义了zipPad3

4 个答案:

答案 0 :(得分:19)

自定义headtail函数(在我的示例中名为nextrest)怎么样?

import Data.Monoid

zipPad :: (Monoid a, Monoid b) => [a] -> [b] -> [(a,b)]
zipPad [] [] = []
zipPad xs ys = (next xs, next ys) : zipPad (rest xs) (rest ys)

zipPad3 :: (Monoid a, Monoid b, Monoid c) => [a] -> [b] -> [c] -> [(a,b,c)]
zipPad3 [] [] [] = []
zipPad3 xs ys zs = (next xs, next ys, next zs) : zipPad3 (rest xs) (rest ys) (rest zs)

next :: (Monoid a) => [a] -> a
next [] = mempty
next xs = head xs

rest :: (Monoid a) => [a] -> [a]
rest [] = []
rest xs = tail xs

测试片段:

instance Monoid Int where
  mempty = 0
  mappend = (+)

main = do
  print $ zipPad [1,2,3,4 :: Int] [1,2 :: Int]
  print $ zipPad3 [1,2,3,4 :: Int] [9 :: Int] [1,2 :: Int]

其输出:

[(1,1),(2,2),(3,0),(4,0)]
[(1,9,1),(2,0,2),(3,0,0),(4,0,0)]

答案 1 :(得分:12)

这种模式出现了很多。我从Paul Chiusano学到的解决方案如下:

data These a b = This a | That b | These a b

class Align f where
  align :: (These a b -> c) -> f a -> f b -> f c

instance Align [] where
  align f []     []     = []
  align f (x:xs) []     = f (This x)    : align f xs []
  align f []     (y:ys) = f (That y)    : align f [] ys
  align f (x:xs) (y:ys) = f (These x y) : align f xs ys

liftAlign2 f a b = align t
  where t (This l)    = f l b
        t (That r)    = f a r
        t (These l r) = f l r

zipPad a b = liftAlign2 (,) a b

liftAlign3 f a b c xs ys = align t (zipPad a b xs ys)
  where t (This  (x,y))   = f x y c
        t (That  r)       = f a b r
        t (These (x,y) r) = f x y r

zipPad3 a b c = liftAlign3 (,,) a b c

ghci中的一个小测试:

 *Main> zipPad3 ["foo", "bar", "baz"] [2, 4, 6, 8] [True, False] "" 0 False
 [("foo",2,True),("bar",4,False),("baz",6,False),("",8,False)]

答案 2 :(得分:4)

更简单的方法是使用Maybe。我将用爱德华的例子来说明 更通用的配方:

import Data.Maybe
import Control.Applicative

zipWithTails l r f as bs = catMaybes . takeWhile isJust $
    zipWith fMaybe (extend as) (extend bs)
  where
    extend xs = map Just xs ++ repeat Nothing
    fMaybe a b = liftA2 f a b <|> fmap l a <|> fmap r b

答案 3 :(得分:3)

有些时候,您希望能够将不同的功能应用于尾部,而不仅仅提供mempty或手动零:

zipWithTail :: (a -> a -> a) -> [a] -> [a] -> [a]
zipWithTail f (a:as) (b:bs) = f a b : zipWithTails f as bs
zipWithTail f [] bs = bs
zipWithTail f as _ = as

zipWithTails :: (a -> c) -> (b -> c) -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithTails l r f (a:as) (b:bs) = f a b : zipWithTails l r f as bs
zipWithTails _ r _ [] bs = fmap r bs
zipWithTails l _ _ as _ = fmap l as

当我做zipWithTail (+)之类的事情时,我使用前者 前者当我需要做zipWithTail (*b) (a*) (\da db -> a*db+b*da)之类的事情时,因为前者比将默认值输入函数要有效得多,而后者则更为有效。

然而,如果你只是想制作一个更简洁的版本,你可能会转向mapAccumL,但它不是更清晰,而且++可能很昂贵。

zipPad as bs = done $ mapAccumL go as bs
    where go (a:as) b = (as,(a,b))
          go [] b = ([],(mempty,b))
          done (cs, both) = both ++ fmap (\x -> (x, mempty)) cs