Re: [Haskell-beginners] function defenition. Do I understand it right?
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@uvm.edu Date: Mon, 11 Jul 2011 15:55:13 -0400 To: rwobben@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....
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@neondsl.com To: rwobben@hotmail.com CC: beginners@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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
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@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@uvm.edu Date: Mon, 11 Jul 2011 15:55:13 -0400 To: rwobben@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....
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@neondsl.com To: rwobben@hotmail.com CC: beginners@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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
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?
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 Date: Mon, 11 Jul 2011 18:56:42 -0400 CC: beginners@haskell.org To: rwobben@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?
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@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 Date: Mon, 11 Jul 2011 18:56:42 -0400 CC: beginners@haskell.org To: rwobben@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 http://www.haskell.org/mailman/listinfo/beginners
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@gmail.com To: rwobben@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 <rwobben@hotmail.com<mailto:rwobben@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: rwobben@hotmail.com<mailto:rwobben@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
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@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@gmail.com To: rwobben@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 <rwobben@hotmail.com<mailto:rwobben@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: rwobben@hotmail.com<mailto:rwobben@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
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@gmail.com To: rwobben@hotmail.com CC: beginners@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@hotmail.com<mailto:rwobben@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@gmail.com<mailto:edwards.benj@gmail.com> To: rwobben@hotmail.com<mailto:rwobben@hotmail.com> CC: beginners@haskell.org<mailto: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
<rwobben@hotmail.com<mailto:rwobben@hotmail.com><mailto:rwobben@hotmail.com<mailto:rwobben@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><mailto:d@vidplace.com<mailto:d@vidplace.com>>
Date: Mon, 11 Jul 2011 18:56:42 -0400 CC: beginners@haskell.org<mailto:beginners@haskell.org><mailto:beginners@haskell.org<mailto:beginners@haskell.org>> To: rwobben@hotmail.com<mailto:rwobben@hotmail.com><mailto:rwobben@hotmail.com<mailto:rwobben@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><mailto:Beginners@haskell.org<mailto:Beginners@haskell.org>>
_______________________________________________ Beginners mailing list Beginners@haskell.org<mailto:Beginners@haskell.org> http://www.haskell.org/mailman/listinfo/beginners
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
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
Hi, On 12 July 2011 16:15, Brent Yorgey <byorgey@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
----------------------------------------
Date: Tue, 12 Jul 2011 09:15:45 -0400 From: byorgey@seas.upenn.edu To: beginners@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@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
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
participants (7)
-
Benjamin Edwards -
Brent Yorgey -
David McBride -
David Place -
Ozgur Akgun -
Roelof Wobben -
Thomas