Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Created September 13, 2020 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kana-sama/634642d19e69049e9500914f89404462 to your computer and use it in GitHub Desktop.
Save kana-sama/634642d19e69049e9500914f89404462 to your computer and use it in GitHub Desktop.
import Control.Arrow (loop)
replaceWithMax, replaceWithMax' :: Ord a => [a] -> [a]
replaceWithMax xs =
let (result, max) = go (xs, max)
in result
where
go ([], replacer) = ([], replacer)
go ([x], replacer) = ([replacer], x)
go ((x:xs), replacer) =
let (xs', replacer') = go (xs, replacer)
in (replacer : xs', max replacer' x)
replaceWithMax' = loop go
where
go ([], replacer) = ([], replacer)
go ([x], replacer) = ([replacer], x)
go ((x:xs), replacer) =
let (xs', replacer') = go (xs, replacer)
in (replacer : xs', max replacer' x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment