Re: library of monadic functions [was: Why no findM ? simple Cat revisited]

2003-11-11 Thread Samuel Tardieu
Andrew J Bromage wrote:

I can also make a case for:

  findM'' :: (Monad m) = (a - Bool) - [m a] - m a
  findM'' p [] = fail findM'': not found
  findM'' p (x:xs) = p x = \b - if b then return x else findM'' p xs
The last line doesn't seem to compile. Don't you mean

findM'' p (x:xs) = x = \b - if p b then return b else findM'' p xs

instead?

  Sam

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: library of monadic functions [was: Why no findM ? simple Cat revisited]

2003-11-11 Thread ajb
G'day all.

Quoting Samuel Tardieu [EMAIL PROTECTED]:

 The last line doesn't seem to compile. Don't you mean
[...]

Err... yes.

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


library of monadic functions [was: Why no findM ? simple Cat revisited]

2002-11-20 Thread Jorge Adriano

 I appreciate your comment.
 I agree that the type of findM should be the one you suggested,
 and it still fits my original purpose. It's no more than a step arout.

 \begin{code}

 import IO
 findM f [] = return Nothing
 findM f (x:xs) = do { b - f x; if b then return (Just x) else findM f xs }

 isLeft (Left _) = True
 isLeft _ = False

 main = findM (=return.isLeft) (hCat stdin)
 where hCat h = try (hGetLine h=putStrLn) : hCat h

 \end{code}

Yes, you are right. 
It was enough because, you don't really care about what you found, you just 
want to search and stop when you do find something. You are returning the 
action that returned an element that satisfied your 
condition, not the actual element like before.

 I expetct the next Haskell Library Report includes findM.
 It's obviously useful.

I think both versions can be very useful:
findM  :: (Monad m) = (a - m Bool) - [a] - m (Maybe a)
findM'  :: (Monad m) = (a - Bool) - [m a] - m (Maybe a)

Same can be said for,
takeWhileM :: (Monad m) = (a - m Bool) - [a] - m [a]
takeWhileM' :: (Monad m) = (a - Bool) - [m a] - m [a]

both would be usefull for different purposes.
Oh and since we're on it I also miss,
iterateM  :: (Monad m) = (a - m a) - a - m [a]
untilM :: (Monad m) = (a - m a) - a - m [a]
etc etc...

I've just been coding them as I need them, like many of you I suppose.
J.A.
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: library of monadic functions [was: Why no findM ? simple Cat revisited]

2002-11-20 Thread Andrew J Bromage
G'day all.

On Wed, Nov 20, 2002 at 08:25:46PM +, Jorge Adriano wrote:

 I think both versions can be very useful:
 findM  :: (Monad m) = (a - m Bool) - [a] - m (Maybe a)
 findM'  :: (Monad m) = (a - Bool) - [m a] - m (Maybe a)

I can also make a case for:

  findM'' :: (Monad m) = (a - Bool) - [m a] - m a
  findM'' p [] = fail findM'': not found
  findM'' p (x:xs) = p x = \b - if b then return x else findM'' p xs

This goes with the philosophy that library functions shouldn't just
return Maybe.

Somewhere, somehow, there is a most general version of findM to be
found. :-)

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe