[Haskell-cafe] Polymorphic type

Sara Kenedy sarakenedy at gmail.com
Thu Jun 22 13:50:21 EDT 2006


Hello all,

Now I am trying with the function of polymorphic type: This function
returns the Nth element of list with type a. I try it as below.

getNthElem :: Int -> [a] -> Maybe a
getNthElemt _ []	= Nothing
getNthElem 0 _	= Nothing
getNthElem n s 	
		| n > length s	= Nothing
		| otherwise 	= Just ((drop (n-1) (take n s))!!0)

>getNthElem 2 ["a","b","c"]
Just "b"

However, I do not satisfy with this function because I want to return
the Nth element of type a, not (Maybe a). For example, I want this
function:
getNthElem :: Int -> [a] ->  a

But, I do not know how to define the empty element of type a.

getNthElemt _ []	= ????
getNthElem 0 _	=  ????

 If you have some ideas about this, please give me some clues. Thanks a lot.

S.


More information about the Haskell-Cafe mailing list