module Lab08a where

separate :: [Int] -> ([Int], [Int])
separate [] = ([], [])
separate [x] = ([x], [])
separate (a : b : as) =
  let (sepleft, sepright) = separate as
   in (a : sepleft, b : sepright)

-- >>> separate [1,2,3,4,5]
-- ([1,3,5],[2,4])

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' :: Int -> Int -> String
numToStr' n radix
  | n < radix = [alphabet !! n]
  | otherwise =
    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 s = split n xs
   in [fromIntegral sm / fromIntegral (length chunk)
      | chunk <- s, length chunk == n, let sm = sum chunk]

averageN' n xs =
  map
    ( \chunk ->
        fromIntegral (sum chunk)
          / fromIntegral (length chunk)
    )
    (split n xs)

-- >>> averageN' 3 [-1,0,1,2,3,4]
-- [0.0,3.0]

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