# function defenition. Do I understand it right?

19 messages
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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:
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 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> >
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 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:
Open this post in threaded view
|

## function defenition. Do I understand it right?

Open this post in threaded view
|

## function defenition. Do I understand it right?

Open this post in threaded view
|

## function defenition. Do I understand it right?

 In reply to this post by David McBride-7 Hi. On 11 July 2011 18:44, David McBride 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:
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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?
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 wrote: > Hi. > > On 11 July 2011 18:44, David McBride 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 >
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 wrote: >> Hi. >> >> On 11 July 2011 18:44, David McBride 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
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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?
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 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:
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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 > > 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  >
Open this post in threaded view
|

## function defenition. Do I understand it right?

Open this post in threaded view
|

## function defenition. Do I understand it right?

 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
Open this post in threaded view
|

## function defenition. Do I understand it right?

Open this post in threaded view
|

## function defenition. Do I understand it right?

 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
Open this post in threaded view
|

## function defenition. Do I understand it right?

 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