module Lab08b where

separate :: [Int] -> ([Int], [Int])
separate [] = ([], [])
separate [x] = ([x], [])
separate (a : b : xs) =
  let sep = separate xs
      sepleft = fst sep
      sepright = snd sep
   in (a : sepleft, b : sepright)

alphabet = "0123456789ABCDEF"

numToStr :: Int -> Int -> String
numToStr n radix =
  if n < radix
    then [alphabet !! n]
    else
      numToStr (n `div` radix) radix
        ++ [alphabet !! (n `mod` radix)]

-- >>> numToStr 52 10
-- >>> numToStr 5 2
-- >>> numToStr 255 16
-- "52"
-- "101"
-- "FF"

split :: Int -> [Int] -> [[Int]]
split _ [] = []
split n xs = take n xs : split n (drop n xs)

-- >>> split 3 [1..10]
-- >>> split 3 [1, 2]
-- >>> split 3 [1, 2, 3]
-- [[1,2,3],[4,5,6],[7,8,9],[10]]
-- [[1,2]]
-- [[1,2,3]]

averageN :: Int -> [Int] -> [Float]
averageN n xs =
  let sp = split n xs
   in map
        ( \chunk ->
            fromIntegral (sum chunk)
              / fromIntegral (length chunk)
        )
        sp

-- >>> averageN 3 [-1, 0, 1, 2, 3]
-- [0.0,2.5]

averageN' :: Int -> [Int] -> [Float]
averageN' n xs =
  [ fromIntegral s
      / fromIntegral l
    | chunk <- split n xs,
      length chunk == n,
      let s = sum chunk,
      let l = length chunk
  ]

copy :: Int -> String -> String
copy 0 s = _
copy n s = _
