function defenition. Do I understand it right?

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

function defenition. Do I understand it right?

Roelof Wobben-2

Hello,

I have made a exercise where I must split a even list.
The answer is :

halve xs = (take n xs  drop n xs)
     where n = length xs 'div' 2

Now I want to change it so i can split even and not even list,.
I thought this can be the answer

halve xs =
         lenght 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


Can this be working ?
I did not check this on GCHI because Im  re - installing my OS.

Regards.

Roelof

     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110711/d5ab7dc4/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

David McBride-7
For simple snippets like this, you can go to
http://codepad.org/ddySVOPr and run your code to see if it works.

And your first version (once corrected for syntax) works on any list
length except for empty lists.

On Mon, Jul 11, 2011 at 10:48 AM, Roelof Wobben <rwobben at hotmail.com> wrote:

> Hello,
>
> I have made a exercise where I must split a even list.
> The answer is :
>
> halve xs = (take n xs? drop n xs)
> ???? where n = length xs 'div' 2
>
> Now I want to change it so i can split even and not even list,.
> I thought this can be the answer
>
> halve xs =
> ???????? lenght 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
>
>
> Can this be working ?
> I did not check this on GCHI because Im? re - installing my OS.
>
> Regards.
>
> Roelof
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2

Hello,

Thanks for the tip.
Im stuck now.

I have this :

halve (xs) | length xs'mod'2 == 0   = (take n xs drop n xs)
           | otherwise = (take n+1 xs  drop n+1 xs
           where n= lenght xs'div'2


main = do
  putStrLn $ show $ halve [1,2,3,4]
  putStrLn $ show $ halve [1,2,3]

But now I get this message :

ERROR line 3 - Syntax error in expression (unexpected keyword "where")

Appearently I can't use where here.

How can I solve this ?

Roelof



> Date: Mon, 11 Jul 2011 11:44:28 -0400
> Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
> From: dmcbride at neondsl.com
> To: rwobben at hotmail.com
> CC: beginners at haskell.org
>
> For simple snippets like this, you can go to
> http://codepad.org/ddySVOPr and run your code to see if it works.
>
> And your first version (once corrected for syntax) works on any list
> length except for empty lists.
>
> On Mon, Jul 11, 2011 at 10:48 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
> > Hello,
> >
> > I have made a exercise where I must split a even list.
> > The answer is :
> >
> > halve xs = (take n xs  drop n xs)
> >      where n = length xs 'div' 2
> >
> > Now I want to change it so i can split even and not even list,.
> > I thought this can be the answer
> >
> > halve xs =
> >          lenght 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
> >
> >
> > Can this be working ?
> > I did not check this on GCHI because Im  re - installing my OS.
> >
> > Regards.
> >
> > Roelof
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110711/b8bece7a/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2

Hello,

 

I had a look at it but I don't think it will solve my problem.

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 the halfs have to be the same lenght.

 

So I use :

 

length xs'div' 2 to figure out where the middle is.

lenght xs'mod'2 to look if it's a even or not even list.

 

So I can use splitat but then the lists will not be the same length if it's a not even list.

 

Roelof



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

> Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
> From: jhenahan at uvm.edu
> Date: Mon, 11 Jul 2011 15:55:13 -0400
> To: rwobben at hotmail.com
>
> You're overcomplicating things. Look at splitAt from Data.List:
>
> http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Data-List.html#v%3AsplitAt
>
> On Jul 11, 2011, at 3:51 PM, Roelof Wobben wrote:
>
> > Hello,
> >
> > Thanks for the tip.
> > Im stuck now.
> >
> > I have this :
> >
> > halve (xs) | length xs'mod'2 == 0 = (take n xs drop n xs)
> > | otherwise = (take n+1 xs drop n+1 xs
> > where n= lenght xs'div'2
> >
> >
> > main = do
> > putStrLn $ show $ halve [1,2,3,4]
> > putStrLn $ show $ halve [1,2,3]
> >
> > But now I get this message :
> >
> > ERROR line 3 - Syntax error in expression (unexpected keyword "where")
> >
> > Appearently I can't use where here.
> >
> > How can I solve this ?
> >
> > Roelof
> >
> >
> >
> > > Date: Mon, 11 Jul 2011 11:44:28 -0400
> > > Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
> > > From: dmcbride at neondsl.com
> > > To: rwobben at hotmail.com
> > > CC: beginners at haskell.org
> > >
> > > For simple snippets like this, you can go to
> > > http://codepad.org/ddySVOPr and run your code to see if it works.
> > >
> > > And your first version (once corrected for syntax) works on any list
> > > length except for empty lists.
> > >
> > > On Mon, Jul 11, 2011 at 10:48 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
> > > > Hello,
> > > >
> > > > I have made a exercise where I must split a even list.
> > > > The answer is :
> > > >
> > > > halve xs = (take n xs drop n xs)
> > > > where n = length xs 'div' 2
> > > >
> > > > Now I want to change it so i can split even and not even list,.
> > > > I thought this can be the answer
> > > >
> > > > halve xs =
> > > > lenght 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
> > > >
> > > >
> > > > Can this be working ?
> > > > I did not check this on GCHI because Im re - installing my OS.
> > > >
> > > > Regards.
> > > >
> > > > Roelof
> > > >
> > > >
> > > > _______________________________________________
> > > > Beginners mailing list
> > > > Beginners at haskell.org
> > > > http://www.haskell.org/mailman/listinfo/beginners
> > > >
> > > >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
>    

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

David McBride-7
You are missing a parenthesis in your otherwise alternative, right
before the where.

There are a few other problems including, you are using single quotes
' instead of back quotes ` for your mod functions, and you don't have
commas in between the elements of your pairs.

Despite that the algorithm needs a bit more work to do what you want,
but you are close:  http://codepad.org/9pA4cLwb

On Mon, Jul 11, 2011 at 5:13 PM, Roelof Wobben <rwobben at hotmail.com> wrote:

>
> Hello,
>
>
>
> I had a look at it but I don't think it will solve my problem.
>
> 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 the halfs have to be the same lenght.
>
>
>
> So I use :
>
>
>
> length xs'div' 2 to figure out where the middle is.
>
> lenght xs'mod'2 to look if it's a even or not even list.
>
>
>
> So I can use splitat but then the lists will not be the same length if it's a not even list.
>
>
>
> Roelof
>
>
>
> ----------------------------------------
>> Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
>> From: jhenahan at uvm.edu
>> Date: Mon, 11 Jul 2011 15:55:13 -0400
>> To: rwobben at hotmail.com
>>
>> You're overcomplicating things. Look at splitAt from Data.List:
>>
>> http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Data-List.html#v%3AsplitAt
>>
>> On Jul 11, 2011, at 3:51 PM, Roelof Wobben wrote:
>>
>> > Hello,
>> >
>> > Thanks for the tip.
>> > Im stuck now.
>> >
>> > I have this :
>> >
>> > halve (xs) | length xs'mod'2 == 0 = (take n xs drop n xs)
>> > | otherwise = (take n+1 xs drop n+1 xs
>> > where n= lenght xs'div'2
>> >
>> >
>> > main = do
>> > putStrLn $ show $ halve [1,2,3,4]
>> > putStrLn $ show $ halve [1,2,3]
>> >
>> > But now I get this message :
>> >
>> > ERROR line 3 - Syntax error in expression (unexpected keyword "where")
>> >
>> > Appearently I can't use where here.
>> >
>> > How can I solve this ?
>> >
>> > Roelof
>> >
>> >
>> >
>> > > Date: Mon, 11 Jul 2011 11:44:28 -0400
>> > > Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
>> > > From: dmcbride at neondsl.com
>> > > To: rwobben at hotmail.com
>> > > CC: beginners at haskell.org
>> > >
>> > > For simple snippets like this, you can go to
>> > > http://codepad.org/ddySVOPr and run your code to see if it works.
>> > >
>> > > And your first version (once corrected for syntax) works on any list
>> > > length except for empty lists.
>> > >
>> > > On Mon, Jul 11, 2011 at 10:48 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
>> > > > Hello,
>> > > >
>> > > > I have made a exercise where I must split a even list.
>> > > > The answer is :
>> > > >
>> > > > halve xs = (take n xs drop n xs)
>> > > > where n = length xs 'div' 2
>> > > >
>> > > > Now I want to change it so i can split even and not even list,.
>> > > > I thought this can be the answer
>> > > >
>> > > > halve xs =
>> > > > lenght 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
>> > > >
>> > > >
>> > > > Can this be working ?
>> > > > I did not check this on GCHI because Im re - installing my OS.
>> > > >
>> > > > Regards.
>> > > >
>> > > > Roelof
>> > > >
>> > > >
>> > > > _______________________________________________
>> > > > Beginners mailing list
>> > > > Beginners at haskell.org
>> > > > http://www.haskell.org/mailman/listinfo/beginners
>> > > >
>> > > >
>> > _______________________________________________
>> > Beginners mailing list
>> > Beginners at haskell.org
>> > http://www.haskell.org/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Ozgur Akgun
In reply to this post by David McBride-7
Hi.

On 11 July 2011 18:44, David McBride <dmcbride at neondsl.com> wrote:

> And your first version (once corrected for syntax) works on any list
> length except for empty lists.
>

Why wouldn't it work for empty lists?

For example with the following definition:
halve xs = let n = length xs `div` 2 in (take n xs, drop n xs)

Best,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110712/897ecf1e/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

David Place
In reply to this post by Roelof Wobben-2
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?


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

David McBride-7
In reply to this post by Ozgur Akgun
Because I tried it and it didn't work.  But it turns out it does work
and it was just an ambiguous variable error from using empty list.
Codepad had a weird error, they must be using an old version of ghc or
something.

On Mon, Jul 11, 2011 at 6:17 PM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:

> Hi.
>
> On 11 July 2011 18:44, David McBride <dmcbride at neondsl.com> wrote:
>>
>> And your first version (once corrected for syntax) works on any list
>> length except for empty lists.
>
> Why wouldn't it work for empty lists?
> For example with the following definition:
> halve xs = let n = length xs `div` 2 in (take n xs, drop n xs)
>
> Best,
> Ozgur
>


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Jack Henahan
Codepad uses Hugs, according to their about page. Makes me terribly glad that the error messages in GHC are so much more informative.

On Jul 11, 2011, at 7:11 PM, David McBride wrote:

> Because I tried it and it didn't work.  But it turns out it does work
> and it was just an ambiguous variable error from using empty list.
> Codepad had a weird error, they must be using an old version of ghc or
> something.
>
> On Mon, Jul 11, 2011 at 6:17 PM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:
>> Hi.
>>
>> On 11 July 2011 18:44, David McBride <dmcbride at neondsl.com> wrote:
>>>
>>> And your first version (once corrected for syntax) works on any list
>>> length except for empty lists.
>>
>> Why wouldn't it work for empty lists?
>> For example with the following definition:
>> halve xs = let n = length xs `div` 2 in (take n xs, drop n xs)
>>
>> Best,
>> Ozgur
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2
In reply to this post by David Place



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 at vidplace.com
> Date: Mon, 11 Jul 2011 18:56:42 -0400
> CC: beginners at haskell.org
> To: rwobben at 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?    

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Benjamin Edwards
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 <rwobben at 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 at vidplace.com
> > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > CC: beginners at haskell.org
> > To: rwobben at 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 at 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/3357faf8/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2

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.benj at gmail.com
> To: rwobben at hotmail.com
> CC: beginners at 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
> <rwobben at hotmail.com<mailto:rwobben at 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 at vidplace.com<mailto:d at vidplace.com>
> > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > CC: beginners at haskell.org<mailto:beginners at haskell.org>
> > To: rwobben at hotmail.com<mailto:rwobben at 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 at haskell.org<mailto:Beginners at haskell.org>
> http://www.haskell.org/mailman/listinfo/beginners 
>      

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Benjamin Edwards
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 <rwobben at 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.benj at gmail.com
> > To: rwobben at hotmail.com
> > CC: beginners at 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
> > <rwobben at hotmail.com<mailto:rwobben at 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 at vidplace.com<mailto:d at vidplace.com>
> > > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > > CC: beginners at haskell.org<mailto:beginners at haskell.org>
> > > To: rwobben at hotmail.com<mailto:rwobben at 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 at haskell.org<mailto:Beginners at haskell.org>
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> Beginners mailing list
> Beginners at 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.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

thomas-2
In reply to this post by Roelof Wobben-2
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



Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2
In reply to this post by Benjamin Edwards


Oke,

 

So I changed everything to this :

 


halve (xs) | null 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]
  putStrLn $ show $ halve []

 

But now I see this message :

 

Error occurred
ERROR line 7 - Unresolved top-level overloading
*** Binding             : main
*** Outstanding context : Show b


 

Roelof

 

________________________________

> Date: Tue, 12 Jul 2011 10:57:04 +0100
> Subject: Re: [Haskell-beginners] function defenition. Do I understand
> it right?
> From: edwards.benj at gmail.com
> To: rwobben at hotmail.com
> CC: beginners at haskell.org
>
> 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
> <rwobben at hotmail.com<mailto:rwobben at 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.benj at gmail.com<mailto:edwards.benj at gmail.com>
> > To: rwobben at hotmail.com<mailto:rwobben at hotmail.com>
> > CC: beginners at haskell.org<mailto:beginners at 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
> >
> <rwobben at hotmail.com<mailto:rwobben at hotmail.com><mailto:rwobben at hotmail.com<mailto:rwobben at 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 at vidplace.com<mailto:d at vidplace.com><mailto:d at vidplace.com<mailto:d at vidplace.com>>
> > > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > > CC:
> beginners at haskell.org<mailto:beginners at haskell.org><mailto:beginners at haskell.org<mailto:beginners at haskell.org>>
> > > To:
> rwobben at hotmail.com<mailto:rwobben at hotmail.com><mailto:rwobben at hotmail.com<mailto:rwobben at 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 at haskell.org<mailto:Beginners at haskell.org><mailto:Beginners at haskell.org<mailto:Beginners at haskell.org>>
> > http://www.haskell.org/mailman/listinfo/beginners 
> >
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org<mailto:Beginners at haskell.org>
> http://www.haskell.org/mailman/listinfo/beginners 
>      

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Benjamin Edwards
I am not sure where your error is coming from.

module Main
? where

halve :: [a] -> ([a],[a])
halve xs | null xs = ([],[])
? ? ? ? ?| otherwise = (take r xs, drop s xs)
?where (p,q) = divMod (length xs) 2
? ? ? ?(r,s) = if q == 0 then (p,p) else (p + 1,p)

works fine for me in ghci and is approximately what you wrote :) For
what it's worth your implementation has a bug in it, you only want to
take an extra item in the odd case.

Ben


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Brent Yorgey-2
In reply to this post by Roelof Wobben-2
On Tue, Jul 12, 2011 at 11:43:10AM +0000, Roelof Wobben wrote:

>
>
> Oke,
>
>  
>
> So I changed everything to this :
>
>  
>
>
> halve (xs) | null xs = ([],[])

You do not need this case.  ([], []) is what halve [] already would
have returned even without this case: length [] == 0, so it would
evaluate to (take 0 [], drop 0 []) which is ([], []).

>            | 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]
>   putStrLn $ show $ halve []
>
>  
>
> But now I see this message :
>
>  
>
> Error occurred
> ERROR line 7 - Unresolved top-level overloading
> *** Binding             : main
> *** Outstanding context : Show b

This message is just because it cannot figure out the type of [] in
'putStrLn $ show $ halve []'.  You can write

  putStrLn $ show $ halve ([] :: [Int])

to give it an explicit type.  It's a bit annoying since we happen to
know that the type of the list makes no difference, but the compiler
can't figure that out.

-Brent


Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Ozgur Akgun
Hi,

On 12 July 2011 16:15, Brent Yorgey <byorgey at seas.upenn.edu> wrote:

> This message is just because it cannot figure out the type of [] in
> 'putStrLn $ show $ halve []'.  You can write
>
>  putStrLn $ show $ halve ([] :: [Int])
>
> to give it an explicit type.  It's a bit annoying since we happen to
> know that the type of the list makes no difference, but the compiler
> can't figure that out.
>

Actually, the type of the list does make a difference due to different show
instances. Try:

putStrLn $ show $ halve ([] :: [Char])

Cheers,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110712/29ef43c8/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

function defenition. Do I understand it right?

Roelof Wobben-2
In reply to this post by Brent Yorgey-2



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

> Date: Tue, 12 Jul 2011 09:15:45 -0400
> From: byorgey at seas.upenn.edu
> To: beginners at haskell.org
> Subject: Re: [Haskell-beginners] function defenition. Do I understand it right?
>
> On Tue, Jul 12, 2011 at 11:43:10AM +0000, Roelof Wobben wrote:
> >
> >
> > Oke,
> >
> >
> >
> > So I changed everything to this :
> >
> >
> >
> >
> > halve (xs) | null xs = ([],[])
>
> You do not need this case. ([], []) is what halve [] already would
> have returned even without this case: length [] == 0, so it would
> evaluate to (take 0 [], drop 0 []) which is ([], []).
>
> > | 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]
> > putStrLn $ show $ halve []
> >
> >
> >
> > But now I see this message :
> >
> >
> >
> > Error occurred
> > ERROR line 7 - Unresolved top-level overloading
> > *** Binding : main
> > *** Outstanding context : Show b
>
> This message is just because it cannot figure out the type of [] in
> 'putStrLn $ show $ halve []'. You can write
>
> putStrLn $ show $ halve ([] :: [Int])
>
> to give it an explicit type. It's a bit annoying since we happen to
> know that the type of the list makes no difference, but the compiler
> can't figure that out.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

 

 

Hello,

 

Problem solved. See this url: http://codepad.org/jMPCO1UE

 

I have tested the difference with  [int] and [Char].

With Int You get this output ([],[]) and with Char this one ["",""]

 

Everyone thanks for the help and patience.

 

Roelof