
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.

On 6/22/06, Sara Kenedy
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.
Not all types (especially numbers) have an empty element (what does that even mean?). Suppose you have a list [0, 1, -2, -1, 2] and you try getNthElemt 4 and your program assumes that the empty element for integers is 0. How can you tell that 0 from the 0 at the beginning of the list [0, 1, 2]? Think really hard about what you are asking and you will see why Maybe a takes the type a and extends it, in a way, with an empty element, Nothing. To convert it from Maybe a to a, try, e.g. fromJust (Just 4) ====> 4 (it will give exceptions when Nothing shows up).
getNthElemt _ [] = ???? getNthElem 0 _ = ????
One possiblity is to make a class called empty with a single member:
class Empty a where empty :: a instance Empty [a] where -- this also makes "" = empty for String empty = [] instance Empty Maybe a where -- is this desirable? empty = Nothing instance Integer where -- or this? empty = 0 ...
and then add the constraint to your function:
getNthElem :: Empty a => Int -> [a] -> a getNthElem :: Int -> [a] -> Maybe a getNthElemt _ [] = empty getNthElem 0 _ = empty getNthElem n s | n > length s = empty | otherwise = ((drop (n-1) (take n s))!!0)
but you need overlapping instances to instantiate [a]. Or you could use MonadPlus and mzero instead of Empty and empty, but that would only work for List, Maybe and other monads and not for Integer, etc. Note that in a dynamic language the same thing happens. In python 4 + None raises an exception. I don't think it's possible to get away from this whole "failure" concept (except silently ignore it---in perl 4+null yields 4 but is that always the right behavior in all situations? It makes bugs really hard to find.) Jared. -- http://www.updike.org/~jared/ reverse ")-:"

2006/6/22, Sara Kenedy
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.
hi, precisely, you want to return an "a" only when there is one accordingly to your above code. the only way to handle this without resorting to [] or Nothing to say there is no such value is to use error or default value. infact, ask yourself, what do you want ?
getNthElem 5 ["a","b","c"] or getNthElem 0 ["a","b","c"] or getNthElem (-1) ["a","b","c"] do you want "a" [] Nothing "wrong" or raise an exception (i.e. you use the "error" function) ?
once you know what you want, you can code it. note, i think your 'take" is unnecessary here
| otherwise = Just ((drop (n-1) (take n s))!!0)
also you can use
| otherwise = Just (n!!some_value) -- :)
this is where you see that the function you're trying to write is really close of
(!!)
cheers, mt

Sara Kenedy wrote:
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.
You might find it's always a lot easier to start counting from zero rather than 1, so that "a" is the 0th element, "b" is the 1st element etc. Just like a building with 2 floors has a ground floor and a first floor, and if you want to find what day of the week it is in 46 days from today you just use (today + 46) `mod` 7 instead of (((today - 1) + 46) `mod` 7) + 1 That aside, why not just throw an error when the function is called with an index that's out of range? getNthElemt _ [] = error "getNthElemt" Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com

Thanks all. I think for my function, I only need to throw an error
message for the out of range index. But through this, I know more some
ways to deal with the polymorphic type.
On 6/22/06, Brian Hulley
Sara Kenedy wrote:
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.
You might find it's always a lot easier to start counting from zero rather than 1, so that "a" is the 0th element, "b" is the 1st element etc. Just like a building with 2 floors has a ground floor and a first floor, and if you want to find what day of the week it is in 46 days from today you just use (today + 46) `mod` 7 instead of (((today - 1) + 46) `mod` 7) + 1
That aside, why not just throw an error when the function is called with an index that's out of range?
getNthElemt _ [] = error "getNthElemt"
Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us.
participants (4)
-
Brian Hulley
-
Jared Updike
-
minh thu
-
Sara Kenedy