[Haskell-beginners] Overload ^

trent shipley trent.shipley at gmail.com
Sat Mar 3 06:24:28 UTC 2018


{-

I want to override Prelude.^ (as an academic exercise).

Can it be done? How?

Trent

-}

(^) :: Int -> Int -> Int
m ^ 0 = 1
m ^ n = m * (m ^ (n-1))

{-
Hutton, Graham. Programming in Haskell (p. 263). Cambridge University
Press. Kindle Edition. 2016.
-}

{-

Result

[1 of 1] Compiling Main             ( ex6_3b.hs, interpreted )

ex6_3b.hs:3:16: error:
    Ambiguous occurrence ‘^’
    It could refer to either ‘Prelude.^’,
                             imported from ‘Prelude’ at ex6_3b.hs:1:1
                             (and originally defined in ‘GHC.Real’)
                          or ‘Main.^’, defined at ex6_3b.hs:2:3
  |
3 | m ^ n = m * (m ^ (n-1))   |                ^
Failed, no modules loaded.
-}

-- So I try

(^) :: Int -> Int -> Int
m Main.^ 0 = 1
m Main.^ n = m * (m Main.^ (n-1))

{-

Result

Prelude> :reload

ex6_3b.hs:2:3: error: Qualified name in binding position: Main.^
  |
2 | m Main.^ 0 = 1    |   ^^^^^^
[1 of 1] Compiling Main             ( ex6_3b.hs, interpreted )
Failed, no modules loaded.

-}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180302/4b30bc68/attachment.html>


More information about the Beginners mailing list