Two type declarations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Two type declarations

Justin Thong
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Two type declarations

Sylvain Henry-2

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners