Jacobi symbol: Difference between revisions
m
→{{header|Haskell}}: Disaggregated the tabulation functions a little
m (→{{header|Haskell}}: Disaggregated the tabulation functions a little) |
|||
Line 288:
p = (3 ==) . flip rem 4
r = rem x y
------------------------- DISPLAY -------------------------▼
jacobiTable :: Int -> Int -> String▼
jacobiTable nCols nRows =▼
withColumnLabels ("" : fmap show [0 .. nCols]) $
labelledRows (fmap show [1,3 .. (2 * nRows)]) $
paddedCols $
chunksOf nRows $
-------------------------- TEST ---------------------------
Line 293 ⟶ 304:
main = putStrLn $ jacobiTable 11 9
▲------------------------- DISPLAY -------------------------
▲jacobiTable :: Int -> Int -> String
------------------ TABULATION FUNCTIONS -------------------
▲jacobiTable nCols nRows =
paddedCols :: Show
paddedCols cols =
let scols = fmap show <$> cols
rjust = justifyRight w ' ' . show▼
labelledRows :: [String] -> [[String]] -> [[String]]
labelledRows labels cols =
let w = maximum $ map length
in zipWith (:) ((++ " ->") . justifyRight w ' ' <$> labels) (transpose cols)
withColumnLabels :: [String] -> [[String]] -> String
▲ (uncurry jacobi <$> ((<*>) . fmap (,)) cols [1,3 .. (nRows * 2)]))
withColumnLabels labels rows@(x:_) =
let labelRow =
in unlines $ labelRow : replicate (length
justifyRight :: Int -> a -> [a] -> [a]
|