[Haskell-beginners] Selecting single result of function application to list

Daniel Schoepe daniel at schoepe.org
Thu Nov 3 17:30:48 CET 2011


On Thu, 03 Nov 2011 16:07:01 +0000, Hugo Ferreira <hmf at inescporto.pt> wrote:
> Hello,
> 
> Apologies the simpleton question but I would like to
> know how it is done in Haskell. I have a list of values,
> and I am applying a function to each of these elements.
> The result is a Maybe. I would like to return the first
> occurrence which is not a Nothing.

You can use catMaybes from Data.Maybe:

import Data.Maybe

selectOne f = head . catMaybes . map f

Note that is a partial function; it will crash if f returns Nothing for
each element.

Cheers,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111103/b041ef51/attachment.pgp>


More information about the Beginners mailing list