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. Re:  function defenition. Do I understand it      right?
      (Benjamin Edwards)
   2. Re:  function defenition. Do I understand it      right? (Thomas)


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

Message: 1
Date: Tue, 12 Jul 2011 10:57:04 +0100
From: Benjamin Edwards <edwards.b...@gmail.com>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
        it      right?
To: Roelof Wobben <rwob...@hotmail.com>
Cc: beginners@haskell.org
Message-ID:
        <can6k4ngix+k4jevuuf7gmzhatz6lmzwzphe3fs97dlquxmp...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Yes, you are totally correct.

your guard for length zero should be ([],[]). The type of the function is

halve :: [a] -> ([a], [a])

so all the code paths have to finish with that type.

Incidentally taking the length of a linked list forces you to walk the
entire list. You are better off checking as to whether it is empty or not.

Regards,
Ben

On 12 July 2011 10:44, Roelof Wobben <rwob...@hotmail.com> wrote:

>
> Oke,
>
>
>
> I have now this as function definition.
>
>
>
>
>
> halve (xs) | length xs `mod` 2 == 0   = (take n xs, drop n xs)
>           | otherwise = (take (n+1) xs,  drop (n+1) xs)
>  where n= length xs `div` 2
>
>
> main = do
>  putStrLn $ show $ halve [1,2,3,4]
>  putStrLn $ show $ halve [1,2,3]
>
>
>
>
> this one works except for empty lists.
>
> So I thought this would work .
>
>
>
> halve (xs) | length xs == 0 = []
>
>           | length xs `mod`2 == 0 = (take n xs, drop n xs)
>
>           | otherwise = (take (n+1) xs, drop (n+1) xs)
>
>   where n = length xs `div`2
>
>
>
>
>
> but then I see this error :
>
>
>
>
>
> Error occurred
> ERROR line 2 - Type error in guarded expression
> *** Term           : (take n xs,drop n xs)
> *** Type           : ([b],[b])
> *** Does not match : [a]
>
>
>
> So I assume that a function must always have the same output and can't have
> 1 or 2 lists as output.
>
>
>
> Is this the right assumption.
>
>
>
> Roelof
>
>
> ________________________________
> > Date: Tue, 12 Jul 2011 10:34:25 +0100
> > Subject: Re: [Haskell-beginners] function defenition. Do I understand
> > it right?
> > From: edwards.b...@gmail.com
> > To: rwob...@hotmail.com
> > CC: beginners@haskell.org
> >
> > I don't even understand what you are trying to do :)
> >
> > if you want to pattern match on the empty list
> >
> > foo :: [a] -> [a]
> > foo [] = 0
> > foo (x:xs) = undefined
> >
> > if you want to use the guard syntax
> >
> > foo xs | null xs = 0
> > | otherwise = undefined
> >
> >
> > Ben
> >
> > On 12 July 2011 10:02, Roelof Wobben
> > <rwob...@hotmail.com<mailto:rwob...@hotmail.com>> wrote:
> >
> >
> >
> > hello
> >
> >
> >
> > Everyone thanks for the help.
> >
> > I'm now trying to make this work on a empty list.
> >
> >
> >
> > But my question is.
> >
> > When the definition is :
> >
> >
> >
> > [a] -> [a] [a]
> >
> >
> >
> > Is it correct that I don't can use.
> >
> >
> >
> > length xs = 0 | []
> >
> >
> >
> > Roelof
> >
> > ----------------------------------------
> > > Subject: Re: [Haskell-beginners] function defenition. Do I understand
> > it right?
> > > From: d...@vidplace.com<mailto:d...@vidplace.com>
> > > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > > CC: beginners@haskell.org<mailto:beginners@haskell.org>
> > > To: rwob...@hotmail.com<mailto:rwob...@hotmail.com>
> > >
> > > On Jul 11, 2011, at 5:13 PM, Roelof Wobben wrote:
> > >
> > > > What I try to achieve is this:
> > > >
> > > >
> > > >
> > > > [1,2,3,4] will be [1,2] [3,4]
> > > >
> > > > [1,2,3,4,5] will be [1,2,3] [3,4,5]
> > >
> > > So, I think what you want is this http://codepad.org/kjpbtLfR
> > >
> > > Is that correct?
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org<mailto:Beginners@haskell.org>
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> 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/20110712/78eae83e/attachment-0001.htm>

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

Message: 2
Date: Tue, 12 Jul 2011 11:57:49 +0200
From: Thomas <hask...@phirho.com>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
        it      right?
To: beginners@haskell.org
Message-ID: <4e1c1a9d.8030...@phirho.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 12.07.2011 11:44, Roelof Wobben wrote:

> So I thought this would work .
> halve (xs) | length xs == 0 = []

This returns a list

>             | length xs `mod`2 == 0 = (take n xs, drop n xs)
>             | otherwise = (take (n+1) xs, drop (n+1) xs)

These return a pair of two lists each.

>
>     where n = length xs `div`2

You probably want to return a pair of empty lists in the first case, too.

HTH,
Thomas




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

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


End of Beginners Digest, Vol 37, Issue 20
*****************************************

Reply via email to