[Haskell-beginners] Overload ^

Paul aquagnu at gmail.com
Sat Mar 3 06:33:11 UTC 2018


import  Prelude hiding ((^))

See here: https://wiki.haskell.org/Import

Best regards,
Paul


От: trent shipley
Отправлено: 3 марта 2018 г. в 8:25
Кому: beginners at haskell.org
Тема: [Haskell-beginners] Overload ^

{-

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/20180303/e4e6fa88/attachment.html>


More information about the Beginners mailing list