Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: When a return type constrained by class (Haisheng Wu) ---------------------------------------------------------------------- Message: 1 Date: Sun, 18 Mar 2012 18:24:20 +0800 From: Haisheng Wu <fre...@gmail.com> Subject: Re: [Haskell-beginners] When a return type constrained by class To: Kyle Murphy <orc...@gmail.com>, allber...@gmail.com Cc: Haskell Beginer <beginners@haskell.org> Message-ID: <cafj8lzek-afk1rrxjo-4qst7ragk4nmi0l+vur84afquace...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Thank you Kyle and Brandon! I am actually re-implementing a toy solution which was done in JavaScript. Turns out I can not make quite clear difference between typeclasses and OOP thus I got this question. I'm also aware making a class for nextGuess would be helpful and I'm still working on. PS: you might like to find the toy program at : https://github.com/freizl/play-hangman-game -Haisheng On Sun, Mar 18, 2012 at 5:13 PM, Kyle Murphy <orc...@gmail.com> wrote: > I'd guess the problem is because nextGuess isn't capable of producing any > instance of Guess. The type signature of a function isn't just to provide > callers information about a function like in most languages, it's also a > contract the function itself must abide by. In contrast it's common in an > OO language like Java to provide a more general return type like an > interface or superclass in the method declaration, and then to return a > more concrete type in the actual method. Haskell in contrast requires that > you be capable of returning *anything* that your functions type signature > claims it can. In your example, nextGuess must be capable of producing > *any* instance of nextGuess that a caller of the function requests. Put > another way, both of these must be valid: > > nextGuess "example" :: GuessLetter > nextGuess "example" :: GuessWord > > Typically this is accomplished by making a function like nextGuess part of > the class as in how Read does it: > > class Read a where > read :: Read a => String -> a > > By definition then, any instance of read must provide a read function > capable of producing something of that type. So, it seems obvious the > solution to your problem is to make nextGuess part of the Guess class. > Alternatively if that's not really what you're trying to accomplish the > type signature of nextGuess is probably wrong. If you provide more detail > on what you're attempting to do maybe someone on here can suggest another > way of accomplishing it. > > -R. Kyle Murphy > -- > Curiosity was framed, Ignorance killed the cat. > > > On Sun, Mar 18, 2012 at 02:23, Haisheng Wu <fre...@gmail.com> wrote: > >> Hi there, >> Do you have any idea why the following code is not valid? >> >> class Guess g where >> makeGuess :: g -> String >> >> instance Guess GuessLetter where >> makeGuess = ... >> >> instance Guess GuessWord where >> makeGuess = ... >> >> *-- | This function is nod valid * >> nextGuess :: Guess g => String -> g >> nextGuess = ... >> >> >> Thank you. >> >> -Haisheng >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120318/46257131/attachment-0001.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 45, Issue 23 *****************************************