Skip to content

Instantly share code, notes, and snippets.

@rampion
Created December 5, 2020 16:15
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 rampion/5c7bf01fae425d553263a7193946ec8f to your computer and use it in GitHub Desktop.
Save rampion/5c7bf01fae425d553263a7193946ec8f to your computer and use it in GitHub Desktop.
"Implement the reverse function using the sort function" from https://bindthegap.news/issues/BindTheGap-01Nov2020.pdf
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module SortAsReverse where
-- challenge from bindthegap.news issue 1, Nov 2020
--
-- implement reverse using sort
import Data.Coerce (coerce)
import Data.List (sort)
-- | reverse a list
--
-- >>> sortAsReverse "0123456789"
-- "9876543210"
--
-- >>> sortAsReverse "9876543210"
-- "0123456789"
--
-- Generates equivalent results (not runtime) to Prelude.reverse
--
-- prop> reverse xs == sortAsReverse xs
--
-- Highly dependent upon the implementation of sort
sortAsReverse :: forall a. [a] -> [a]
sortAsReverse = coerce (sort @(Rev a))
newtype Rev a = Rev a
instance Ord (Rev a) where
compare _ _ = GT
instance Eq (Rev a) where
_ == _ = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment