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 |
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? You probably want final to have type final :: [a] -> a But the equation final [] = [] conflicts with this. BTW, you might want to have this kind of discussion at [hidden email] instead. See the announcement: http://thread.gmane.org/gmane.comp.lang.haskell.general/16345 Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:[hidden email] _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Fernando Rodriguez-6
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 > Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Fernando Rodriguez-6
2008/7/21 Fernando Rodriguez <[hidden email]>:
> 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? As Janis said final [] = [] conflicts with the signature you want for final BUT the definition of final is still typeable, it just don't have the type you think it have... and combined with the way Haskell handle number literals you get this confusing error. final's type is [[a]] -> [a] because final [] = [] imply that the return type should be a list and thus (final [a] = a) imply that the input list elements should themselves be lists (because a should be a list since it is returned in this case)... So final wants a list of list. All is well until now and if you try for example : > final [True, False] you'll get a slightly better error message (maybe hard for newbies but very understandable with a little bit of experience) : Couldn't match expected type `[a]' against inferred type `Bool' In the expression: True In the first argument of `final', namely `[True, False]' In the expression: final [True, False] Now your error message is particularly nasty because all integer literals like "5" are treated in the following fashion in Haskell : they're implicitly translated to "fromInteger 5", fromInteger being a function of the Num typeclass that for an instance Num a take an Integer and translate it to the a type. This translation is normally pretty obvious with some bound checking (for Int type) and simple translation (to Double or Float) but you could in theory have some pretty crazy instances of Num, [a] for example. For example here Haskell complains that there are no Num instances for the [a] type since that's what he want to translate the "5" into.... I hope you understood this explanation, even if it's a little bit crazy and blury for now, just remember, most of the time if you have an error like : No instance for (Num X) arising from the literal `5' at <interactive>:1:9 you can more or less translate it to : Couldn't match expected type `X' against inferred type `Integer' (This is only true for the Num typeclass, since this is the only case of polymorphic literals you'll meet for now). -- Jedaï _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Fernando Rodriguez-6
Hi Fernando,
> 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] The problem is that final has the type [a] -> a, so you cannot have a pattern match that simply returns the empty list; this makes the function partial because taking the tail of an empty list is undefined. If you wish to avoid it, you can wrap it in a Maybe: final :: [a] -> Maybe a final [] = Nothing final [a] = Just a final (_:t) = final t $ cat > final.hs final :: [a] -> Maybe a final [] = Nothing final [a] = Just a final (_:t) = final t $ ghci final.hs GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( final.hs, interpreted ) Ok, modules loaded: Main. *Main> final [1,2] Just 2 *Main> final [] Nothing *Main> Austin _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by C.M.Brown
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. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Free forum by Nabble | Edit this page |