[Haskell-beginners] making function problem (chapter 6 of Programming in Haskell)
Ertugrul Soeylemez
es at ertes.de
Mon Aug 8 13:26:14 CEST 2011
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.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
More information about the Beginners
mailing list