Imaginary base numbers: Difference between revisions

m
→‎{{header|Haskell}}: (Tidying suggested types)
m (→‎{{header|Haskell}}: Suggestion (OP may prefer to revert): signatures restored, hlint and hindent applied, imports specified.)
m (→‎{{header|Haskell}}: (Tidying suggested types))
Line 1,089:
import Data.List (elemIndex, delete)
import Data.Maybe (fromMaybe)
 
 
base :: Complex Float
base = 0 :+ 2
 
quotRemPositive :: Int -> Int -> (Int, Int)
:: (Integral a)
=> a -> a -> (a, a)
quotRemPositive a b
| r < 0 = (1 + q, floor (realPart (-base ^^ 2)) + r)
Line 1,102 ⟶ 1,100:
where
(q, r) = quotRem a b
 
digitToIntQI :: Char -> Int
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10
 
shiftRight :: String -> String
shiftRight n
Line 1,114 ⟶ 1,112:
where
(l, h) = (last n, init n)
 
intToDigitQI :: Int -> Char
intToDigitQI i
| i `elem` [0 .. 9] = intToDigit i
| otherwise = chr (i - 10 + ord 'a')
 
fromQItoComplex :: String -> Complex Float -> Complex Float
fromQItoComplex num b =
Line 1,129 ⟶ 1,127:
(0, 1)
(delete '.' num)
 
euclidEr :: Int -> Int -> [Int] -> [Int]
euclidEr a b l
Line 1,136 ⟶ 1,134:
let (q, r) = quotRemPositive a b
in euclidEr q b (0 : r : l)
 
fromIntToQI :: Int -> [Int]
fromIntToQI 0 = [0]
fromIntToQI x = tail (euclidEr x (floor $ realPart (base ^^ 2)) [])
 
getCuid :: Complex Int -> Int
:: Integral a
=> Complex a -> a
getCuid c = imagPart c * floor (imagPart (-base))
 
qizip :: Complex Int -> [Int]
qizip c =
Line 1,153 ⟶ 1,149:
take (length i - m) i ++
reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
 
fromComplexToQI :: Complex Int -> String
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip
 
main :: IO ()
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
9,655

edits