Meaning of (Eq a)

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

Meaning of (Eq a)

Rohit Garg
Hi,

RWH: chapter 3 - in question 5, you have to write a function which
determines if a list is a palindrome. Here is my solution

isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome [] = False
isPalindrome x = compareLists x (reverse x)
    where compareLists [x] [y]       = x == y
          compareLists (x:xs) (y:ys) = if x == y
                                       then compareLists xs ys
                                       else False

Although it works, my question is why ghci refuses to run it without
the "(Eq a) => " being added to the type signature of the function.
Presumably, it is to let ghc know that you can perform equlity tests
on a. If so, then why does the sumList function below work without any
type signature of any kind? I haven't told ghc that the input list
elements can be added together.

sumList [] = 0
sumList (x:xs) = x + sumList xs


Thanks,
--
Rohit Garg

http://rpg-314.blogspot.com/
Reply | Threaded
Open this post in threaded view
|

Meaning of (Eq a)

Johan Jeuring
> RWH: chapter 3 - in question 5, you have to write a function which
> determines if a list is a palindrome. Here is my solution
>
> isPalindrome :: (Eq a) => [a] -> Bool
> isPalindrome [] = False
> isPalindrome x = compareLists x (reverse x)
>    where compareLists [x] [y]       = x == y
>          compareLists (x:xs) (y:ys) = if x == y
>                                       then compareLists xs ys
>                                       else False
>
> Although it works, my question is why ghci refuses to run it without
> the "(Eq a) => " being added to the type signature of the function.
> Presumably, it is to let ghc know that you can perform equlity tests
> on a. If so, then why does the sumList function below work without any
> type signature of any kind? I haven't told ghc that the input list
> elements can be added together.
>
> sumList [] = 0
> sumList (x:xs) = x + sumList xs

Maybe GHC infers this for you?

Inspect the type of sumList by means of

 >:t sumList

Cheers,

Johan
Reply | Threaded
Open this post in threaded view
|

Meaning of (Eq a)

Lakshmi Narasimhan-2
In reply to this post by Rohit Garg
  On 09/05/2010 02:28 PM, Rohit Garg wrote:

> Hi,
>
> RWH: chapter 3 - in question 5, you have to write a function which
> determines if a list is a palindrome. Here is my solution
>
> isPalindrome :: (Eq a) =>  [a] ->  Bool
> isPalindrome [] = False
> isPalindrome x = compareLists x (reverse x)
>      where compareLists [x] [y]       = x == y
>            compareLists (x:xs) (y:ys) = if x == y
>                                         then compareLists xs ys
>                                         else False
>
> Although it works, my question is why ghci refuses to run it without
> the "(Eq a) =>  " being added to the type signature of the function.
> Presumably, it is to let ghc know that you can perform equlity tests
> on a. If so, then why does the sumList function below work without any
> type signature of any kind? I haven't told ghc that the input list
> elements can be added together.
>
> sumList [] = 0
> sumList (x:xs) = x + sumList xs
>
>
> Thanks,
Hi Rohit,
You are correct about the assumption. Giving (Eq a) is constraining the
set of types that can be used with to the palindrome function. The
elements of the type a can be tested for equality.

The compiler can perform type inference. It will find out the type
signature if you do not provide one. Assume that you did not provide the
type signature for  the isPalindrome function, here is what I get when I
loaded this code into ghci.
     *Main> :t isPalindrome
     isPalindrome :: (Eq t) => [t] -> Bool

For the sumList function, since you did not provide the type
information, the compiler will infer the type for the argument and
result of the function. However, as you point out correctly, not all
types can be added. Hence  constraint (Num t) will be added to the type
signature. This means that the elements  of the type "t"  can be added.
If not, then that type cannot be used and  a compiler error will result.

     sumList :: (Num t) => [t] -> t

Hope that helps
-Lakshmi Narasimhan
Reply | Threaded
Open this post in threaded view
|

Meaning of (Eq a)

Lakshmi Narasimhan-2
Actually (Num t)  would mean that certain other functions are also
applicable to the elements of the type t, not only addition.

On Sun, Sep 5, 2010 at 2:49 PM, Lakshmi Narasimhan <
[hidden email]> wrote:

> This means that the elements  of the type "t"  can be added.




--
Regards
Lakshmi Narasimhan T V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100905/c43e221a/attachment.html
Reply | Threaded
Open this post in threaded view
|

Meaning of (Eq a)

Brandon S Allbery KF8NH
In reply to this post by Rohit Garg
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 9/5/10 04:58 , Rohit Garg wrote:
> Although it works, my question is why ghci refuses to run it without
> the "(Eq a) => " being added to the type signature of the function.

Haskell won't do type inference if you provide a function with a type
signature, so if you say anything at all you need to say everything about
it.  If you say *nothing* about it, as with the second function, Haskell
will infer everything including required constraints.

- --
brandon s. allbery     [linux,solaris,freebsd,perl]      [hidden email]
system administrator  [openafs,heimdal,too many hats]  [hidden email]
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyDrYwACgkQIn7hlCsL25U60ACguFkSUsfyPdk77Q7AmA5McVcu
0S0An3lbVUtVyzGCd6SSer2bksufokpd
=NAqj
-----END PGP SIGNATURE-----