haskellsquare-root

Haskell code for square root of negative integers


How can I use Haskell to find the square root of a negative integer?


Solution

  • You can use the sqrt :: Floating a => a -> a function [Hackage] where you work with a Complex type [Hackage]:

    ghci> sqrt (-4) :: Complex Float
    0.0 :+ 2.0
    ghci> sqrt 4 :: Complex Float
    2.0 :+ 0.0

    so this means 0 + 2i and 2 + 0i respectively.