[Haskell-cafe] Re: Coin changing algorithm

Tomasz Zielonka tomasz.zielonka at gmail.com
Thu Jul 14 06:51:40 EDT 2005


On Thu, Jul 14, 2005 at 01:44:05PM +0300, Yitzchak Gale wrote:
> Here it is translated into regular monad syntax:
> 
> > coinsM _  0 k = return []
> > coinsM _  _ 0 = []
> > coinsM cs a k = do
> >   t <- init (tails cs)
> >   let h = head t
> >   unless (h <= a) []
> >   s <- coinsM t (a-h) (k-1)
> >   return (h:s)
> 
> (The function "unless" is from the Control.Monad
> module.)

[] is an instance of MonadPlus, which allows you to use "guard" instead
of "unless ...":

    guard (h <= a)

Best regards
Tomasz


More information about the Haskell-Cafe mailing list