[Haskell-beginners] newbie: Monad, equivalent notation using Control.Monad.guard

Hugo Ferreira hmf at inescporto.pt
Mon Oct 17 17:18:05 CEST 2011


Hello,

I came across the following code:

ngrams'' :: Int -> [a] -> [[a]]
ngrams'' n l = do
   t <- Data.List.tails l
   l <- [take n t]
   Control.Monad.guard (length l == n)
   return l

and tried to use the ">>=" operator in order
to figure out how Monads work. I came up with:

test l =
    (Data.List.tails l)
    >>= (\t -> [take 2 t])
    >>= (\l -> if (length l == 2) then [l] else [])

Questions:
1. How can I use Control.Monad.guard directly in "test l"
2. Related issue: how can create a List monad so that I
    can input "l" argument of "test l" directly?

TIA,
Hugo F.



More information about the Beginners mailing list