acme-strtok- A Haskell port of the C/PHP strtok function

LicensePublic Domain
MaintainerManuel Eberl <last name + m _at_>
Safe HaskellNone




This module provides the function strTok, a variant of the strtok function in C and PHP. This function can be used to tokenise a string (or, more generally, a list) with successive calls of the strtok function. Since strTok is a stateful function (it produces different results when called with the same parameter multiple times), computations using strTok must take place in the StrTok monad or the StrTokT monad transformer.


The StrTokT monad transformer

data StrTokT s m a Source

The StrTokT monad, parametrised with:

  • s - The type of list elements (e.g. Char if the input to strTok is a String).
  • m - The inner monad.


MonadTrans (StrTokT s) 
(Functor m, MonadPlus m) => Alternative (StrTokT s m) 
Monad m => Monad (StrTokT s m) 
Functor m => Functor (StrTokT s m) 
MonadFix m => MonadFix (StrTokT s m) 
MonadPlus m => MonadPlus (StrTokT s m) 
(Monad m, Functor m) => Applicative (StrTokT s m) 
MonadIO m => MonadIO (StrTokT s m) 

runStrTokT :: Functor m => StrTokT s m a -> m a Source

Executes a strTok computation in the state transformer monad StrTokT.

The StrTok monad

type StrTok s = StrTokT s Identity Source

The StrTok monad.

runStrTok :: StrTok s a -> a Source

Executes a strTok computation in the state monad StrTok.

The strTok function

strTok :: (Eq a, Monad m) => Maybe [a] -> [a] -> StrTokT a m [a] Source

A Haskell variant of the strtok function from C and PHP. This function splits a string into tokens which are delimited by a given set of characters. A call with Just s and the delimiting characters ds will yield the first token in s that is delimited by characters from ds. Every subsequent call of strTok with Nothing will yield the next token. If the string contains no more tokens, an empty list is returned.

strTok returns a stateful computation of type StrTokT a m [a] (or StrTok a [a]). Several invocations of strTok and computations with the results can be chained in the StrTokT (resp. StrTok) monad and then executed with runStrTokT (resp. runStrTok).


runStrTokT $
      do a <- strTok (Just "- This, a sample string.") " ,.-"
         b <- strTok Nothing " ,.-"
         c <- strTok Nothing ",.-"
         return (a, b, c)

evaluates to

("This","a"," sample string")