module Data.List.NonEmpty where

-- | Tries to convert a list to a NonEmpty. Returns the NonEmpty if it can, or fails in the monad otherwise.
listToNonEmpty :: Monad m => [a] -> m (NonEmpty a)
listToNonEmpty s = do { (h:t) <- return s; return $ NonEmpty { neHead = h, neTail = t } }

-- | A non-empty list
data NonEmpty a
    = NonEmpty { neHead :: a
               , neTail :: [a]
               }

-- | Semantic function for NonEmpty
nonEmptyToList :: NonEmpty a -> [a]
nonEmptyToList ne = neHead ne : neTail ne

-- | Length of a given NonEmpty
length' :: NonEmpty a -> Int
length' = length . nonEmptyToList