<commit_before>module Chapter5
where

import Chapter4 (rjustify)

binom :: Int -> Int -> Int
binom n k
  | n < 0 =
    0
  | k == 0 =
    1
  | otherwise =
    binom (n - 1) k + binom (n - 1) (k - 1)


pascal :: Int -> [[Int]]
pascal x =
  [map toBinom xs | xs <- table]
  where
    toBinom (n, k) =
      binom n k

    elemCount =
      x + 1

    table =
      [take elemCount (drop n elems) | n <- [0, elemCount..(length elems - elemCount)]]

    elems =
      [(a, b) | a <- [0..x], b <- [0..x]]


printPascal :: Int -> IO()
printPascal x =
  mapM_ putStr (printableTable (pascal x))
  where
    printableTable :: [[Int]] -> [String]
    printableTable xss =
      map (printableRow (rjustify (longestDigit xss + 1))) xss

    printableRow rjustified xs =
      foldr ((++) . rjustified  . show) "\n" xs

    longestDigit :: [[Int]] -> Int
    longestDigit =
      -- maximum . concat . map (map digits)
      maximum . map localLongestDigit

    localLongestDigit :: [Int] -> Int
    localLongestDigit =
      maximum . map digits

    digits :: Integral a => Int -> a
    digits n =
      floor $ logBase 10.0 (fromIntegral n) + 1<commit_msg>Improve printing of pascal table<commit_after>module Chapter5
where

import Chapter4 (rjustify)

binom :: (Int, Int) -> Int
binom (n, k)
  | n < 0 =
    0
  | k == 0 =
    1
  | otherwise =
    binom ((n - 1), k) + binom ((n - 1), (k - 1))


pascal :: Int -> [[Int]]
pascal x =
  [map binom xs | xs <- xss]
  where
    elemCount =
      x + 1

    xss =
      [take elemCount (drop n elems) | n <- [0, elemCount..(length elems - elemCount)]]

    elems =
      [(a, b) | a <- [0..x], b <- [0..x]]


printPascal :: Int -> IO()
printPascal x =
  mapM_ putStr table
  where
    pascalTriangle =
      pascal x

    table =
      header ++ ["\n"] ++ divisor ++ ["\n"] ++ content

    justification =
      1 + (longestDigit pascalTriangle)

    divisor =
      take (2 + (length pascalTriangle + 1) * justification) (repeat "-")

    content =
      map row (zip pascalTriangle [0..length pascalTriangle])

    row (r, idx) =
      rowIndex idx ++ rowContent (rjustify justification) r

    rowIndex idx =
      (rjustify justification (show idx)) ++ " |"

    header =
      ["  " ++ rjustify justification "|"] ++ map ((rjustify justification) . show) [0..length pascalTriangle - 1]

    rowContent rjustified xs =
      foldr ((++) . rjustified . showNonZero) "\n" xs

    showNonZero y =
      if y > 0 then show y else " "

    longestDigit :: [[Int]] -> Int
    longestDigit =
      -- maximum . concat . map (map digits)
      maximum . map localLongestDigit

    localLongestDigit :: [Int] -> Int
    localLongestDigit =
      maximum . map digits

    digits :: Integral a => Int -> a
    digits n =
      floor $ logBase 10.0 (fromIntegral n) + 1
{'fraction_non_alphanumeric': 0.14209210996072832, 'fraction_numerical': 0.014994644769725098, 'mean_word_length': 2.1237458193979935, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2797446', 'n_tokens_mistral': 1058, 'n_tokens_neox': 974, 'n_words': 305}