[Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Roelof Wobben
rwobben at hotmail.com
Mon Aug 8 16:28:28 CEST 2011
----------------------------------------
> To: beginners at haskell.org
> From: es at 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 <rwobben at hotmail.com> 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
More information about the Beginners
mailing list