[Haskell-beginners] function defenition. Do I understand it right?
David McBride
dmcbride at neondsl.com
Mon Jul 11 23:49:10 CEST 2011
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
>
More information about the Beginners
mailing list