Created
December 5, 2020 16:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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