[Haskell-beginners] Two type declarations

Justin Thong justinthong93 at gmail.com
Wed Mar 28 18:19:35 UTC 2018


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.html>


More information about the Beginners mailing list