Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Complex
  2.  
  3. newtype Cmplx = Cmplx (Complex Double) deriving Eq
  4.  
  5. instance Show Cmplx where
  6.   show (Cmplx z) = (show $ realPart z) ++ (if im >= 0 then "+" else "-") ++ "i*" ++ (show $ abs im) where im = imagPart z
  7.  
  8. instance Read Cmplx where
  9.   readsPrec _ = myReadsCmplx
  10.  
  11. myReadsCmplx s = [(Cmplx (x :+ y), t) | (x,u) <- readRealPart s, (y,t) <- readImagPart u]
  12.  
  13. readRealPart s = reads s :: [(Double, String)]
  14. readImagPart ('+':'i':'*':s) = reads s :: [(Double, String)]
  15. readImagPart ('-':'i':'*':s) = [(-re,u) | (re,u) <- reads s] :: [(Double, String)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement