NonEmpty(非空列表)

infixr 5 :|

data NonEmpty a = a :| [a]
deriving (Eq, Ord) instance Functor NonEmpty where
fmap f ~(a :| as) = f a :| fmap f as
b <$ ~(_ :| as) = b :| (b <$ as) instance Applicative NonEmpty where
pure a = a :| []
(<*>) = ap
liftA2 = liftM2 instance Monad NonEmpty where
~(a :| as) >>= f = b :| (bs ++ bs')
where b :| bs = f a
bs' = as >>= toList . f
toList ~(c :| cs) = c : cs

NonEmpty是一个非空的list类型,支持大多数 list 类型的操作。

Prelude> import Data.List.NonEmpty as NE
Prelude NE> a = 5 :| []
Prelude NE> :t a
a :: Num a => NonEmpty a
Prelude NE> NE.head a
5
Prelude NE> NE.tail a
[]
Prelude NE> b = 6 <| a
Prelude NE> b
6 :| [5]
Prelude NE> NE.map (+2) b
8 :| [7]
Prelude NE> 3 <$ b
3 :| [3]
Prelude NE> fromList [1,2,3]
1 :| [2,3]
Prelude NE> toList it
[1,2,3]