Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/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. Two type declarations (Justin Thong) 2. Re: Two type declarations (Sylvain Henry) ---------------------------------------------------------------------- Message: 1 Date: Wed, 28 Mar 2018 14:19:35 -0400 From: Justin Thong <justinthon...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Two type declarations Message-ID: <CAEtAGerrqzKtfwh5P=ovzj98crqpvquwcup2hug_j09k1d3...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" What is the difference between these two type declarations? The second one is wrong but I can't convince myself why it should be wrong. Is it because *Int *not a constraint class and it is only an instance of one? My curiousity is why #1 can't be written in the form of #2. I apologise if I am using wrong terminology as type, class and constraint class are used with not much distinction. To add context, this problem is to find a function that will find an element by passing in a list and an index argument. elementAt''' ::[a]-> Int ->a -- #1 elementAt''' [] _= error "list is empty" elementAt''' list index | (index < 1) = error "index has to be positive number" | otherwise= list !! (index-1) elementAt'''' ::(Int b)=>[a]-> b ->a -- #2 elementAt'''' [] _= error "list is empty" elementAt'''' list index | (index < 1) = error "index has to be positive number" | otherwise= list !! (index-1) Thank you. I just began learning Haskell. Yours sincerely, Justin *I check my email at 9AM and 4PM everyday* *If you have an EMERGENCY, contact me at +447938674419(UK) or +60125056192(Malaysia)* -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180328/5856e98c/attachment-0001.html> ------------------------------ Message: 2 Date: Wed, 28 Mar 2018 20:33:56 +0200 From: Sylvain Henry <sylv...@haskus.fr> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Two type declarations Message-ID: <fd372e70-e7c4-327d-4df0-99b9b7d46...@haskus.fr> Content-Type: text/plain; charset="utf-8"; Format="flowed" Indeed "Int b" is not a valid constraint: the kind of "Int" is Type (or "*") as GHC reports: > Expecting one fewer arguments to ‘Int’ > Expected kind ‘* -> Constraint’, but ‘Int’ has kind ‘*’ A valid constraint would be "Int ~ b" as in the following example. But I don't see why you would do this in this case, especially if you are beginning with Haskell. It complicates the code for no gain. {-# LANGUAGE TypeFamilies #-} elementAt'''' ::(Int ~ b)=>[a]-> b ->a -- #2 elementAt'''' [] _= error "list is empty" elementAt'''' list index | (index < 1) = error "index has to be positive number" | otherwise= list !! (index-1) Cheers Sylvain On 28/03/2018 20:19, Justin Thong wrote: > What is the difference between these two type declarations? The second > one is wrong but I can't convince myself why it should be wrong. Is it > because /Int /not a constraint class and it is only an instance of > one? My curiousity is why #1 can't be written in the form of #2. I > apologise if I am using wrong terminology as type, class and > constraint class are used with not much distinction. To add context, > this problem is to find a function that will find an element by > passing in a list and an index argument. > > elementAt''' ::[a]-> Int ->a -- #1 > elementAt''' [] _= error "list is empty" > elementAt''' list index > | (index < 1) = error "index has to be positive number" > | otherwise= list !! (index-1) > > elementAt'''' ::(Int b)=>[a]-> b ->a -- #2 > elementAt'''' [] _= error "list is empty" > elementAt'''' list index > | (index < 1) = error "index has to be positive number" > | otherwise= list !! (index-1) > > Thank you. I just began learning Haskell. > > > Yours sincerely, > Justin > > /I check my email at 9AM and 4PM everyday/ > /If you have an *EMERGENCY*, contact me at +447938674419(UK) or > +60125056192(Malaysia)/ > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180328/151e225a/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 117, Issue 16 ******************************************