[Haskell-cafe] What is the point of many and some functions in Control.Applicative (for Alternative?)

Dan Doel dan.doel at gmail.com
Sat Jul 3 16:06:55 EDT 2010


On Saturday 03 July 2010 3:57:34 pm Thomas Hartman wrote:
> When I load up Control.Applicative in ghci and try, eg
> 
> many [1,2] or many (Just 1) or some [1,2] or some (Just 1)
> 
> this never returns.
> 
> What are the practical uses of these combinators, or for using the
> Alternative class in general?

  import Control.Applicative
  import Control.Monad
  import Control.Monad.State

  type M = StateT [Int] []

  pluck :: M Int
  pluck = do l <- get
             case l of
                  []   -> empty
                  x:xs -> put xs *> pure x

  {-

  *Main> runStateT (many pluck) [1..4]
  [([1,2,3,4],[]),([1,2,3],[4]),([1,2],[3,4]),([1],[2,3,4]),([],[1,2,3,4])]

  -}

-- Dan


More information about the Haskell-Cafe mailing list