Overload ^

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Overload ^

trent shipley
{-

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.

-}

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Overload ^

Paul

import  Prelude hiding ((^))

 

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

 

Best regards,

Paul

 

 

От: [hidden email]
Отправлено: 3 марта 2018 г. в 8:25
Кому: [hidden email]
Тема: [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.

 

-}

 


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners