Hi Fernando,
I hope you don't mind, but I've moved this over to the Haskell-beginners mailing list, where I think this kind of question will be more appropriate. In Haskell, it helps to think of functions in terms of an input and an output, that is, what is the thing that is going into the function; and what is the thing that is coming out of the function? In your function, final, the input is clearly a list of something (we can denote this by the Haskell type [a] which means a list of some type, a). Its return type is clearly an element of the list, so that must be the something type (the 'a' from the [a]). This gives the Haskell type: final :: [a] -> a (final takes a list of 'a' and gives back a single 'a'. The 'a' is a type variable, and it is used to denote that anything can be put in its place, so we can give final a list of integers, characters, whatever). Now, let's take a look at your definition of final. If we take a closer look, in fact only two equations satisfy this type: final [a] = a final (_:t) = final t The other takes a list and returns a list. The equation, final [] = [] takes an empty list and returns an empty list (its type is therefore [a] -> [a]). This is why you got an error, as Haskell doesn't know how what to do with the conflicting equation. What we need is the final element of the list. How do we do that? Let's think of the simple cases first. The final element of a list containing a single element is just that element, final [a] = a But what about if the list contains more elements? Or is an empty list? The empty list may be confusing, as an empty list contains no elements, so in effect, we can't return anything. We can, however, return an error message. fun [] = error "empty List" And the final element of any list, must be the final element of its tail: final (_:t) = final t this gives us: final :: [a] -> a final [] = error "Empty List" final [a] = a final (_:t) = final t I hope that gives some insight. Kind regards, Chris. On Mon, 21 Jul 2008, Fernando Rodriguez wrote: > > Hi, > > I defiend the following function to get the last element of a list: > > final [a] = a > final (_:t) = final t > > and it works as expected. Since I didn't want to have a non exhaustive pattern, > I added the following case: > > final [] = [] - I consider that the end of an empty list is the empty list > final [a] = a > final (_:t) = final t > > Suddenly, the function stoped working with a rather cryptic (for a newbie > at least) error message: > > *Temp> final [4,5] > > <interactive>:1:9: > No instance for (Num [a]) > arising from the literal `5' at <interactive>:1:9 > Possible fix: add an instance declaration for (Num [a]) > In the expr*Temp> ession: 5 > In the first argument of `final', namely `[4, 5]' > In the expression: final [4, 5] > > What have I done so wrong? > > Thanks in advance, > Fernando > > > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe > |
Just to avoid any misunderstanding...
I am certain that C.M. Brown meant to say "CC'ed the Haskell-beginners mailing list" instead of "moved", but I think it's worth emphasizing that the new beginners list was ostensibly created for various discussed reasons, but all to provide a more tailored forum for beginners, not to restrict participation on haskell-cafe. Words like "move" could sound to a beginner like a dismissal or demotion. (This policy is clearly different from the haskell list, which has a much stronger collegially-enforced moderation policy limited to announcements.) I would hate to think that people on the beginners list might worry that their questions were not "good enough" to join the "grown-ups" on haskell-cafe. I think CC'ing to beginners is hint enough, and soon enough people will choose the best forum for their comfort level. Dan C.M.Brown wrote: > Hi Fernando, > > I hope you don't mind, but I've moved this over to the Haskell-beginners > mailing list, where I think this kind of question will be more > appropriate. |
Free forum by Nabble | Edit this page |