[Haskell-beginners] matching the output of a standard function to my function definition (non-exhaustive pattern problem)

Brent Yorgey byorgey at seas.upenn.edu
Mon Sep 27 12:08:59 EDT 2010


On Mon, Sep 27, 2010 at 05:41:24PM +0200, Martin Tomko wrote:
> Dear all
> 
> I am sure this is a very beginner problem, but I am not sure how to
> solve it:
> I have a myFunct function:
> myFunct :: Int -> [a] -> a
> 
> defined as:
> myFunct _ [] = error "empty list provided as arg"
> myFunct a [b] | length [x|x<-[b],fid x == a] == 0 = error "no match"
>                       |    otherwise = head [x|x<-[b],fid x == a]

May I also suggest that instead of implementing this yourself, you
could just use

  myFunct a b = find ((==a) . fid) b

'find' is from Data.List and returns the first thing in the list
satisfying the given predicate, or Nothing if there is no such
element.

-Brent


More information about the Beginners mailing list