Skip to content

Instantly share code, notes, and snippets.

@viercc
Created March 9, 2021 09:09
Show Gist options
  • Select an option

  • Save viercc/aa52e1cfeeb250e28b6e43df7a5f80e1 to your computer and use it in GitHub Desktop.

Select an option

Save viercc/aa52e1cfeeb250e28b6e43df7a5f80e1 to your computer and use it in GitHub Desktop.
Answering effectfully<HYPHEN>ou/haskell<HYPHEN>challenges<SLASH>h4-largest-powers
module Lib
( largestPowersInt ) where
class Eq a => Iterable a where
zer :: a
inc :: a -> a
dec :: a -> a
instance Iterable Int where
zer = 0
inc = succ
dec = pred
largestPowersInt :: Int -> [Int]
largestPowersInt = largestPowers
largestPowers :: Iterable a => Int -> [a]
largestPowers n = aux (inc zer)
where aux k = spread (n-1) k (aux (inc k))
-- > spread 2 '.' "apple" = "..a..p..p..l..e.."
spread :: Int -> a -> [a] -> [a]
spread n space as = spaces ++ (as >>= \a -> a : spaces)
where spaces = replicate n space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment