Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Why the length function I wrote has such a   type
      signature? (amazingjxq)
   2. Re:  Why the length function I wrote has such a   type
      signature? (Daniel Fischer)
   3. Re:  Why the length function I wrote has such     a type
      signature? (Isaac Dupree)
   4. Re:  Why the length function I wrote has such a   type
      signature? (amazingjxq)
   5. Re:  a simple problem (Markus L?ll)
   6. Re:  a simple problem (Brent Yorgey)


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

Message: 1
Date: Thu, 11 Nov 2010 16:55:20 +0800
From: amazingjxq <[email protected]>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
        such a  type signature?
To: Chadda? Fouch? <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

myLength :: [a] -> Int

This is the first type signature I wrote. And I changed the Int into Num
after
ghci tell me it's wrong. This type signature still not work. But the
standard length
function's type signature is this:
length :: [a] -> Int

I think my type signature is right but it's not. And I can not find the
reason.

2010/11/11 Chaddaï Fouché <[email protected]>

> On Thu, Nov 11, 2010 at 9:24 AM, 贾旭卿 <[email protected]> wrote:
> > This is exercise 3.1 of Real World Haskell. I have my length function
> like
> > this:
> >
> > myLength [] = 0
> > myLength (_:xs) = 1 + (myLength xs)
> >
> > And I assumed the type signature is like this:
> > mylength :: [a] -> Num
> >
> > But when I wrote this into the file and reloaded it into ghci, there is
> an
> > error.
> >>
> >>     The type signature for `mylength' lacks an accompanying binding
> >> Failed, modules loaded: none.
> >
> >
> > And the type signature given by ghci is
> >>
> >> myLength :: (Num t1) => [t] -> t1
> >
> > So how can I modify the function to have a type signature like the first
> > one?
>
> You can't, since Num isn't a type, it's a typeclass.
>
> > myLength :: (Num b) => [a] -> b
>
> means that myLength takes a list of any type and can return any type
> that is an instance of Num (Num being the typeclass of numbers, that
> means that you can do most things you do on numbers, adding them,
> multiplying them, and so on...).
>
> If you want a simpler type signature, you could use :
>
> > myLength :: [a] -> Int
>
> or
>
> > myLength :: [a] -> Integer
>
> since Int (32 or 64 bits integer) and Integer are real type that are
> instances of the Num typeclass.
>
> --
> Jedaï
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101111/19af372b/attachment-0001.html

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

Message: 2
Date: Thu, 11 Nov 2010 10:04:58 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
        such a  type signature?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 11 November 2010 09:24:44, 贾旭卿 wrote:
> This is exercise 3.1 of Real World Haskell. I have my length function
> like this:
>
> myLength [] = 0
> myLength (_:xs) = 1 + (myLength xs)
>
> And I assumed the type signature is like this:
> mylength :: [a] -> Num
>
> But when I wrote this into the file and reloaded it into ghci, there is
> an error.
>
> >     The type signature for `mylength' lacks an accompanying binding
> > Failed, modules loaded: none.

That's because you mistyped the function name in the type signature, the 
funcion has an uppercase L, the signature got a lowercase l.
Without that typo, you'd have gotten an error:

    Class `Num' used as a type
    In the type signature for `myLength': myLength :: [a] -> Num

which would probably have helped.

>
> And the type signature given by ghci is
>
> > myLength :: (Num t1) => [t] -> t1
>
> So how can I modify the function to have a type signature like the first
> one?

Jedaï answered that.



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

Message: 3
Date: Thu, 11 Nov 2010 04:11:36 -0500
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
        such    a type  signature?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

On 11/11/10 03:24, 贾旭卿 wrote:
> This is exercise 3.1 of Real World Haskell. I have my length function like
> this:
>
> myLength [] = 0
> myLength (_:xs) = 1 + (myLength xs)
>
> And I assumed the type signature is like this:
> mylength :: [a] ->  Num
>
> But when I wrote this into the file and reloaded it into ghci, there is an
> error.
>
>>      The type signature for `mylength' lacks an accompanying binding
>> Failed, modules loaded: none.

In addition to what Chaddaï Fouché said, this particular error is simply 
due to writing "mylength" when it should be "myLength" (L uppercase).

-Isaac


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

Message: 4
Date: Thu, 11 Nov 2010 18:01:30 +0800
From: amazingjxq <[email protected]>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
        such a  type signature?
To: Isaac Dupree <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Thanks everyone. I didn't even think about it.

2010/11/11 Isaac Dupree <[email protected]>

> On 11/11/10 03:24, 贾旭卿 wrote:
>
>> This is exercise 3.1 of Real World Haskell. I have my length function like
>> this:
>>
>> myLength [] = 0
>> myLength (_:xs) = 1 + (myLength xs)
>>
>> And I assumed the type signature is like this:
>> mylength :: [a] ->  Num
>>
>> But when I wrote this into the file and reloaded it into ghci, there is an
>> error.
>>
>>      The type signature for `mylength' lacks an accompanying binding
>>> Failed, modules loaded: none.
>>>
>>
> In addition to what Chaddaï Fouché said, this particular error is simply
> due to writing "mylength" when it should be "myLength" (L uppercase).
>
> -Isaac
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101111/6765b0b0/attachment-0001.html

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

Message: 5
Date: Thu, 11 Nov 2010 18:59:01 +0200
From: Markus L?ll <[email protected]>
Subject: Re: [Haskell-beginners] a simple problem
To: Anonymous W <[email protected]>, [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Anonymous W!

As list is a recursive datastructure, you need a recursive (or
folding) function to check for duplicates. Right now your function
really does accept only two-element lists.

The function would look something like this:

maybeA list@ ((a1,b1) : (a2,b2) : rest) =
   if hasUniqueKeys list
      then Just (a1, b1, b2)
      else Nothing
   where
      hasUniqueKeys xs = ... check the list ...

You also need to decide what to do, if the list is empty or contains
only one pair.


Markus Läll

On Thu, Nov 4, 2010 at 12:31 AM, Anonymous W <[email protected]> wrote:
> Hi,
> I'm having trouble with this small exercise.
>
> A function maybeA takes a list of pairs. A pair has a variable 'a' and a
> value 'b'. If any first element of a pair equals to any first element of
> another pair, the function returns Nothing. If they aren't equal, the
> function returns a tuple containing the first variable, its value and the
> value of the second variable.
> maybeA :: [(a,b)] -> Maybe (a,b,b)
>
>
> For example:  [(a1,b1),(a2,b2),(a3,b3)..] returns Nothing if a2 = a3
> and returns Just (a2,b2,b3) if  a2 /= a3
>
>
>
> I'm currently have :
>
> maybeA [(a1,b1),(a2,b2)]
>
>     |a1 == a2 = Nothing
>
>     |a1 /= a2 = Just (x1,y1,y2)
>
> but it only takes 2 pairs. Any helps?
>
>
>
> Thanks
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

Message: 6
Date: Thu, 11 Nov 2010 12:02:42 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] a simple problem
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Thu, Nov 11, 2010 at 06:59:01PM +0200, Markus Läll wrote:
> Hi Anonymous W!
> 
> As list is a recursive datastructure, you need a recursive (or
> folding) function to check for duplicates. Right now your function
> really does accept only two-element lists.
> 
> The function would look something like this:
> 
> maybeA list@ ((a1,b1) : (a2,b2) : rest) =
>    if hasUniqueKeys list
>       then Just (a1, b1, b2)
>       else Nothing
>    where
>       hasUniqueKeys xs = ... check the list ...
> 
> You also need to decide what to do, if the list is empty or contains
> only one pair.

This is good advice.  I would add, however, that I don't think
hasUniqueKeys ought to be (directly) recursive.  Instead, use
functions from the standard libraries to manipulate the list and
decide whether it has unique keys.  Functions which may be helpful in
this task include map, fst, sort, group, length, any.

-Brent


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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 29, Issue 16
*****************************************

Reply via email to