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.  Type signature question (Louis-Guillaume Gagnon)
   2. Re:  Type signature question (Matthew Lefavor)
   3. Re:  Type signature question (Brandon Allbery)
   4. Re:  Type signature question (Denis Kasak)
   5. Re:  Type signature question (Anindya Mozumdar)
   6. Re:  Type signature question (Erik Price)
   7. Re:  Type signature question (Erik Price)


----------------------------------------------------------------------

Message: 1
Date: Tue, 23 Jul 2013 10:33:35 -0400
From: Louis-Guillaume Gagnon <louis.guillaume.gag...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Type signature question
Message-ID:
        <CANCBZJuJqHUc2+=iupiclecmvuctstyxovyc3uo54m542pj...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hello beginners,

I'm working on the second set of exercises in ch.03 of Real World Haskell

I wrote a palindromize function (:: [a] -> [a]) which transforms a
list in a palindrome (eg. [1,2,3] -> [1,2,3,3,2,1]) .

I wrote a isPalindrome function which checks whether a given list is
such a palindrome.
it reads:
isPalindrome xs
     | odd (length xs)              = False
     | firstHalf == secondHalf =True
     | otherwise                       = False
     where half              = div (length xs) 2
                firstHalf       = take half xs
                secondHalf = reverse (drop half xs)

I would expect the type signature to be:
isPalindrome :: [a] -> Bool

but ghci gives me
is Eq a => [a] -> Bool

and I don't undestand why the "Eq a =>" shows up.

many thanks,

glg



------------------------------

Message: 2
Date: Tue, 23 Jul 2013 10:44:12 -0400
From: Matthew Lefavor <mclefa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID:
        <cajqezs3bpomk_obbe2rr-s2xiib+wisagobj1c8qls3yg7e...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Tue, Jul 23, 2013 at 10:33 AM, Louis-Guillaume Gagnon <
louis.guillaume.gag...@gmail.com> wrote:

> isPalindrome xs
>      | odd (length xs)              = False
>      | firstHalf == secondHalf =True
>      | otherwise                       = False
>      where half              = div (length xs) 2
>                 firstHalf       = take half xs
>                 secondHalf = reverse (drop half xs)
>
> I would expect the type signature to be:
> isPalindrome :: [a] -> Bool
>
> but ghci gives me
> is Eq a => [a] -> Bool
>
> and I don't undestand why the "Eq a =>" shows up
>

"Eq a" is the "Type class" of "a". It means that "a" is a type that
supports the checking of equality.

Other type classes include: "Show" (types that are printable), and "Ord"
(types that are orderable).

Cheers,

MCL
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130723/16c6787c/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 23 Jul 2013 10:46:48 -0400
From: Brandon Allbery <allber...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID:
        <CAKFCL4Xwj1kpvKc=i7zkvrewdhl--ycetppnt7twrx3p+gw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Tue, Jul 23, 2013 at 10:33 AM, Louis-Guillaume Gagnon <
louis.guillaume.gag...@gmail.com> wrote:

> I wrote a isPalindrome function which checks whether a given list is
> such a palindrome.
> it reads:
> isPalindrome xs
>      | odd (length xs)              = False
>      | firstHalf == secondHalf =True
>      | otherwise                       = False
>      where half              = div (length xs) 2
>                 firstHalf       = take half xs
>                 secondHalf = reverse (drop half xs)
>
> I would expect the type signature to be:
> isPalindrome :: [a] -> Bool
>
> but ghci gives me
> is Eq a => [a] -> Bool
>
> and I don't undestand why the "Eq a =>" shows up.
>

It shows up because of `firstHalf == secondHalf`; you used (==), this
requires an Eq constraint so that the type-appropriate implementation of
(==) is available.

-- 
brandon s allbery kf8nh                               sine nomine associates
allber...@gmail.com                                  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130723/f8aa25d8/attachment-0001.html>

------------------------------

Message: 4
Date: Tue, 23 Jul 2013 16:48:21 +0200
From: Denis Kasak <denis.ka...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID:
        <CANJrnZe=BMN68DZ766DGqAb7BVyg8GiXMy_qXBHz=w8kvrt...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 23 July 2013 16:33, Louis-Guillaume Gagnon
<louis.guillaume.gag...@gmail.com> wrote:
>
> and I don't undestand why the "Eq a =>" shows up.

Prelude> :type (==)
(==) :: Eq a => a -> a -> Bool

The (==) operator is a member of the typeclass Eq. This enables many
different types to use the (==) operator by implementing their own
instance of Eq (i.e. their own version of the (==) function). Since
you're using the (==) operator in your code, the only types that can
typecheck are those that have an instance of the Eq typeclass. ghci
detects this and automatically adds the "Eq a =>" which is called a
/class constraint/. It restricts the type variable 'a' to only those
types that have an Eq instance defined.

--
Denis Kasak



------------------------------

Message: 5
Date: Tue, 23 Jul 2013 20:25:15 +0530
From: "Anindya Mozumdar" <anin...@anindyamozumdar.com>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell'" <beginners@haskell.org>,
        <louis.guillaume.gag...@gmail.com>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID: <000c01ce87b4$a74eb7a0$f5ec26e0$@anindyamozumdar.com>
Content-Type: text/plain;       charset="us-ascii"

>>but ghci gives me
>>is Eq a => [a] -> Bool
>>
>>and I don't undestand why the "Eq a =>" shows up.

You are doing the comparison firstHalf == secondHalf. Two lists are equal
only if each element in the same position in the list are equal (and the
lists are of equal length). Thus, you require that the elements of the list
can be compared for equality. A type can be tested for equality only if it
belongs to the Eq typeclass. Thus, automatic type inference in Haskell is
putting this class constraint.

Regards,
Anindya




------------------------------

Message: 6
Date: Tue, 23 Jul 2013 10:57:52 -0400
From: Erik Price <erikpr...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID:
        <cad+x7c8qwsvsxfjn9uqtsgtkmpjznqmof3gsj5xdjqksg1r...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

It's saying that you can't pass parameters to this function unless they are
of the Eq type class, because somewhere in the body of the function is code
that expects parameters to be of this type class.

e


On Tue, Jul 23, 2013 at 10:33 AM, Louis-Guillaume Gagnon <
louis.guillaume.gag...@gmail.com> wrote:

> Hello beginners,
>
> I'm working on the second set of exercises in ch.03 of Real World Haskell
>
> I wrote a palindromize function (:: [a] -> [a]) which transforms a
> list in a palindrome (eg. [1,2,3] -> [1,2,3,3,2,1]) .
>
> I wrote a isPalindrome function which checks whether a given list is
> such a palindrome.
> it reads:
> isPalindrome xs
>      | odd (length xs)              = False
>      | firstHalf == secondHalf =True
>      | otherwise                       = False
>      where half              = div (length xs) 2
>                 firstHalf       = take half xs
>                 secondHalf = reverse (drop half xs)
>
> I would expect the type signature to be:
> isPalindrome :: [a] -> Bool
>
> but ghci gives me
> is Eq a => [a] -> Bool
>
> and I don't undestand why the "Eq a =>" shows up.
>
> many thanks,
>
> glg
>
> _______________________________________________
> 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/20130723/9a604bba/attachment-0001.html>

------------------------------

Message: 7
Date: Tue, 23 Jul 2013 10:59:01 -0400
From: Erik Price <erikpr...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type signature question
Message-ID:
        <CAD+X7c9tEy1F=AjS6Dbn07XWKyL8-YUfTA=kt8xfvhjuqyc...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

My reply is a bit ambiguous, so to clarify: the parameter doesn't have to
be of the Eq type class, because the parameter is a list. But the list's
elements have to be of the Eq type class.

e


On Tue, Jul 23, 2013 at 10:57 AM, Erik Price <erikpr...@gmail.com> wrote:

> It's saying that you can't pass parameters to this function unless they
> are of the Eq type class, because somewhere in the body of the function is
> code that expects parameters to be of this type class.
>
> e
>
>
> On Tue, Jul 23, 2013 at 10:33 AM, Louis-Guillaume Gagnon <
> louis.guillaume.gag...@gmail.com> wrote:
>
>> Hello beginners,
>>
>> I'm working on the second set of exercises in ch.03 of Real World Haskell
>>
>> I wrote a palindromize function (:: [a] -> [a]) which transforms a
>> list in a palindrome (eg. [1,2,3] -> [1,2,3,3,2,1]) .
>>
>> I wrote a isPalindrome function which checks whether a given list is
>> such a palindrome.
>> it reads:
>> isPalindrome xs
>>      | odd (length xs)              = False
>>      | firstHalf == secondHalf =True
>>      | otherwise                       = False
>>      where half              = div (length xs) 2
>>                 firstHalf       = take half xs
>>                 secondHalf = reverse (drop half xs)
>>
>> I would expect the type signature to be:
>> isPalindrome :: [a] -> Bool
>>
>> but ghci gives me
>> is Eq a => [a] -> Bool
>>
>> and I don't undestand why the "Eq a =>" shows up.
>>
>> many thanks,
>>
>> glg
>>
>> _______________________________________________
>> 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/20130723/a2c672d4/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 61, Issue 26
*****************************************

Reply via email to