making function problem (chapter 6 of Programming in Haskell)

Hello, After a short holiday I now studying chapter 6 of this book. For the first exercise I have to make the function for ^ for postitive numbers. So I did these steps. Step 1 : Defining the type ^ :: [Int] -> Int Step 2 : enumarate the cases ^ [] = ^ [n, 0] = ^ [n, ns] = Step 3 : Define the simple cases ^ [] = [] ^ [n.0] = 1 Step 4 : Define the other cases ^[n.ns] = n * ^ns Step 5 : generalize and simplify : ^[] = [] ^[-, 0] = 1 So the whole function would be : ^ :: [Int] -> Int ^[] = [] ^[-, 0] = 1 ^ [n,ns] = n * ^ns But the answer in the book only says : m ^ 0 = 1 m (n+1) = m * m ^n so no type defenition and a whole different solution to the last rule. So my question is : Is what I did wrong ? Roelof

Roelof Wobben
After a short holiday I now studying chapter 6 of this book.
For the first exercise I have to make the function for ^ for postitive numbers.
I assume you mean the exponentiation function, and the only real indication for that is the solution you quoted later in your post. To help us help, you really should work on your problem descriptions.
Step 1 : Defining the type
^ :: [Int] -> Int
First of all, you have to learn proper Haskell syntax. Your type signature is invalid. But let me rewrite your type signature to correct syntax: (^) :: [Int] -> Int This is the type signature for a function (^), which expects exactly one argument, a list. Is this really what you want? Before going any further, you should come up with the right type signature for your function. Once you have that, we will continue. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 12:51:34 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: After a short holiday I now studying chapter 6 of this book.
For the first exercise I have to make the function for ^ for postitive numbers.
I assume you mean the exponentiation function, and the only real indication for that is the solution you quoted later in your post. To help us help, you really should work on your problem descriptions.
Step 1 : Defining the type
^ :: [Int] -> Int
First of all, you have to learn proper Haskell syntax. Your type signature is invalid. But let me rewrite your type signature to correct syntax:
(^) :: [Int] -> Int
This is the type signature for a function (^), which expects exactly one argument, a list. Is this really what you want? Before going any further, you should come up with the right type signature for your function. Once you have that, we will continue.
oke, I don't think I want that. I want to type this 2^3 and then the outcome will be 8. So next try (^) :: Int -> Int -> Int Because the first and second numbers are integers and the outcome also will be a integer. Roelof

Roelof Wobben
I don't think I want that. I want to type this 2^3 and then the outcome will be 8. So next try
(^) :: Int -> Int -> Int
Because the first and second numbers are integers and the outcome also will be a integer.
This looks more like it. Now forget about programming for a moment and think about how you can express the exponentiation operation in terms of simple equations. Let me show you how you would do that for addition of natural numbers, if you have only successor (succ) and predecessor (pred) functions available: x + 0 = x x + y = succ x + pred y Evaluation shows: 3 + 2 = succ 3 + pred 2 = 4 + 1 = succ 4 + pred 1 = 5 + 0 = 5 A similarly simple ruleset can express exponentiation, too. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

Besides the programming problem for (^) for natural numbers; there is also the algorithmic question in that can the recursive case be done better than m ^ n = m * m ^ (n -1)? -- -- Regards, KC

----------------------------------------
To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 13:26:14 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: I don't think I want that. I want to type this 2^3 and then the outcome will be 8. So next try
(^) :: Int -> Int -> Int
Because the first and second numbers are integers and the outcome also will be a integer.
This looks more like it. Now forget about programming for a moment and think about how you can express the exponentiation operation in terms of simple equations. Let me show you how you would do that for addition of natural numbers, if you have only successor (succ) and predecessor (pred) functions available:
x + 0 = x x + y = succ x + pred y
Evaluation shows:
3 + 2 = succ 3 + pred 2 = 4 + 1 = succ 4 + pred 1 = 5 + 0 = 5
A similarly simple ruleset can express exponentiation, too.
Hello, I think you mean something like this : 2 ^ 3 = = 2 * 2 ^ 2 = 2 * 2 * 2 ^1 = 2 * 2 * 2 * 2 ^ 0 = 2 * 2 * 2 * 1 = 2 * 2 * 2 = 4 * 2 = 8 Roelof

Roelof Wobben
I think you mean something like this :
2 ^ 3 = = 2 * 2 ^ 2 = 2 * 2 * 2 ^1 = 2 * 2 * 2 * 2 ^ 0 = 2 * 2 * 2 * 1 = 2 * 2 * 2 = 4 * 2 = 8
Yes, but that's not a rule system. Try to come up with a rule system to express exponentiation. You need two rules for the algorithm you want to implement. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

----------------------------------------
To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 16:36:18 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: I think you mean something like this :
2 ^ 3 = = 2 * 2 ^ 2 = 2 * 2 * 2 ^1 = 2 * 2 * 2 * 2 ^ 0 = 2 * 2 * 2 * 1 = 2 * 2 * 2 = 4 * 2 = 8
Yes, but that's not a rule system. Try to come up with a rule system to express exponentiation. You need two rules for the algorithm you want to implement.
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Maybe this : x ^ 0 = 1 x ^ y = x * (y-1) Roelof

Roelof Wobben
Maybe this :
x ^ 0 = 1 x ^ y = x * (y-1)
No, that's wrong. Don't try guessing things, because that will bring you nowhere with Haskell. But at least you are getting closer to the idea of solving things through algebraic rules. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

----------------------------------------
To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 17:17:18 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: Maybe this :
x ^ 0 = 1 x ^ y = x * (y-1)
No, that's wrong. Don't try guessing things, because that will bring you nowhere with Haskell.
But at least you are getting closer to the idea of solving things through algebraic rules.
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Im not guessing. Im trying to understand what you mean by exponation rules. As far as I can imagaging it cannot be done the same way as you described the sum problem. Roelof

Roelof Wobben
Im not guessing.
Im trying to understand what you mean by exponation rules.
As far as I can imagaging it cannot be done the same way as you described the sum problem.
Try with multiplication first. If you can do multiplication, then exponentiation should be clear. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

----------------------------------------
To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 17:28:08 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: Im not guessing.
Im trying to understand what you mean by exponation rules.
As far as I can imagaging it cannot be done the same way as you described the sum problem.
Try with multiplication first. If you can do multiplication, then exponentiation should be clear.
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Oke, Just to be sure. I only have to use suc and pred ? I think you want something like this : x + 0 = x x + y = succ x + pred y Roelof

----------------------------------------
From: rwobben@hotmail.com To: beginners@haskell.org Date: Mon, 8 Aug 2011 15:35:55 +0000 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
----------------------------------------
To: beginners@haskell.org From: es@ertes.de Date: Mon, 8 Aug 2011 17:28:08 +0200 Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
wrote: Im not guessing.
Im trying to understand what you mean by exponation rules.
As far as I can imagaging it cannot be done the same way as you described the sum problem.
Try with multiplication first. If you can do multiplication, then exponentiation should be clear.
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Oke,
Just to be sure.
I only have to use suc and pred ?
I think you want something like this :
x + 0 = x x + y = succ x + pred y
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
oke, Maybe you are looking something like this : x * 0 = 0 0 * y = 0 x * y = z Roelof

On 08/08/11 16:17, Ertugrul Soeylemez wrote:
Maybe this :
x ^ 0 = 1 x ^ y = x * (y-1) Hi Roelof,
I don't think you're far away - it could just be a typo/lack of care in writing it down. Have you tried putting some actual values into these rules? - that should help you understand what is wrong with what you've written down/get you closer to the correct solution. For example: (x=2, y=4) 2^4 = 16 != 6 = 2 * 3 David.

----------------------------------------
Date: Mon, 8 Aug 2011 19:18:55 +0100 From: dbeacham@dbeacham.co.uk To: beginners@haskell.org Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
On 08/08/11 16:17, Ertugrul Soeylemez wrote:
Maybe this :
x ^ 0 = 1 x ^ y = x * (y-1) Hi Roelof,
I don't think you're far away - it could just be a typo/lack of care in writing it down. Have you tried putting some actual values into these rules? - that should help you understand what is wrong with what you've written down/get you closer to the correct solution.
For example: (x=2, y=4) 2^4 = 16 != 6 = 2 * 3
David.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Oke, what Im trying to say it this : 2 ^ 4 = 2 * 2 ^ 3 = 2 * 2 * 2 ^ 2 = 2 * 2 * 2 * 2 ^ 1 = 2 * 2 * 2 * 2 * 2 ^ 0 So the y is on each number one less on each run. So maybe it's x ^ y = x * x ^ y Roelof

Hint positivePower :: Int -> Int -> Int positivePower x 1 = x positivePower x y = dododoo positivePower yadayada -- -- Regards, KC

----------------------------------------
Date: Mon, 8 Aug 2011 14:59:39 -0700 From: kc1956@gmail.com To: beginners@haskell.org Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Hint
positivePower :: Int -> Int -> Int positivePower x 1 = x positivePower x y = dododoo positivePower yadayada
-- -- Regards, KC
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
oke. I see it I'm going for this : postivePower :: Int -> Int -> Int PostivePower x 1 = 1 because when you multiply something by one is remains the same value. PostivePower x y = x + postivePower x y for example x = 4 y = 3 4 * 3 = 4 + 4 * 2 4 + 4 + 4 * 1 4 + 4 + 4 4 + 8 12 Roelof

Hi Roelof, On Tue, 2011-08-09 at 12:16 +0000, Roelof Wobben wrote:
oke.
I see it
unfortunately its still not true. Your example with the concrete numbers is right, and it captures the scheme well. But try to check your code with it. I'd like to say its typos, but its actually the same typos as you had for the actual main example, calculating the lists. Try to answer the questions I ask below and then use the answers for a correct definition (that is very similar to the recent one).
I'm going for this :
postivePower :: Int -> Int -> Int
PostivePower x 1 = 1 because when you multiply something by one is remains the same value.
Does this really return the value you want? Is 5 * 1 = 1 ?
PostivePower x y = x + postivePower x y
Do you really want to call positivePower with the same y again, or more concretely: Is 4 * 3 = 4 + (4 * 3)?, or should there be a "2" somewhere?
for example
x = 4
y = 3
4 * 3 =
4 + 4 * 2
4 + 4 + 4 * 1
4 + 4 + 4
4 + 8
12

----------------------------------------
Subject: Re: [Haskell-beginners] making function problem (chapter 6 of Programming in Haskell) From: ds@iai.uni-bonn.de To: rwobben@hotmail.com CC: beginners@haskell.org Date: Tue, 9 Aug 2011 14:41:13 +0200
Hi Roelof,
On Tue, 2011-08-09 at 12:16 +0000, Roelof Wobben wrote:
oke.
I see it
unfortunately its still not true. Your example with the concrete numbers is right, and it captures the scheme well. But try to check your code with it. I'd like to say its typos, but its actually the same typos as you had for the actual main example, calculating the lists. Try to answer the questions I ask below and then use the answers for a correct definition (that is very similar to the recent one).
I'm going for this :
postivePower :: Int -> Int -> Int
PostivePower x 1 = 1 because when you multiply something by one is remains the same value.
Does this really return the value you want? Is 5 * 1 = 1 ?
PostivePower x y = x + postivePower x y
Do you really want to call positivePower with the same y again, or more concretely: Is 4 * 3 = 4 + (4 * 3)?, or should there be a "2" somewhere?
It should be 2 so it would then be postivepower x y = x + postivepower x (y-1) Roelof

On Tue, 2011-08-09 at 12:44 +0000, Roelof Wobben wrote:
PostivePower x y = x + postivePower x y
Do you really want to call positivePower with the same y again, or more concretely: Is 4 * 3 = 4 + (4 * 3)?, or should there be a "2" somewhere?
It should be 2 so it would then be postivepower x y = x + postivepower x (y-1)
Right! That's it. Now it's hopefully not a problem to write the (^) function with the same scheme. Cheers, Daniel.
participants (5)
-
Daniel Seidel
-
David Beacham
-
Ertugrul Soeylemez
-
KC
-
Roelof Wobben