Skip to content

Instantly share code, notes, and snippets.

@phadej
Created May 19, 2022 19:18
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 phadej/2a185790c4b151c6f868dc92ffb5aaf9 to your computer and use it in GitHub Desktop.
Save phadej/2a185790c4b151c6f868dc92ffb5aaf9 to your computer and use it in GitHub Desktop.
import Data.Coerce
import Data.Monoid
-- This combinator is based on ala' from Conor McBride's work on Epigram.
alaf :: (Coercible n o, Coercible n' o')
=> (o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> b -> o'
alaf _ = coerce
myfoldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
myfoldl f z xs = alaf (Dual . Endo) foldMap (flip f) xs z
--
-- >>> myreverse "keep it simple"
-- "elpmis ti peek"
--
myreverse :: [a] -> [a]
myreverse = myfoldl (flip (:)) []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment