haskell - How to define 'special' behavior for composite function in `.hs` file? -


composition normal in haskell, know can define special behavior of composite function, says

prelude> (floor . sqrt) (10^55) 3162277660168379365112938496 prelude> let (floor . sqrt) n | n < 2 = n | otherwise = head $ dropwhile (\x -> x^2 > n) $ iterate (\x -> (x + n `div` x) `div` 2) (n `div` 2) prelude> (floor . sqrt) (10^55) 3162277660168379331998893544 

the result special define function correct (because of floating point error in first one).

now want same inside .hs file, like

(floor . sqrt) n     | n < 2 = n     | otherwise = head $ dropwhile (\x -> x^2 > n)                        $ iterate (\x -> (x + n `div` x) `div` 2) (n `div` 2)  main =     print $ (floor . sqrt) (10^55) 

this time ghc yelling @ me

ambiguous occurrence `.' refer either `main..', defined @ me.hs:1:8                       or `prelude..',                          imported `prelude' @ me.hs:1:1                          (and defined in `ghc.base') 

so possible define function in .hs file? (define inside main let okey, however).

first of all, example in ghci not defining special composition of floor , sqrt. instead, it's defining operator (.) takes 3 arguments named floor, sqrt, , n , hides existing standard function named (.).

you apply new function standard library functions floor , sqrt, become parameters same names in new (.) function.

the error because top-level definition not automatically hide existing definition, , anyway that's not want do.

now, can define entirely new function (hopefully own name, in groovy's answer) specialized floor . sqrt function, there's no way in haskell define specialized version of existing functions.

what might thinking of, , possible, using compiler pragma rewrite rules in ghc automatically replace specific expressions equivalent, improved versions. should careful rewritten form gives same answer replaces, though. otherwise you're risking baffling debugging sessions.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

node.js - Bad Request - node js ajax post -

uitableview - Create and use custom prototype table cells in xamarin ios using storyboard -