praxis

Various programming exercises.
Log | Files | Refs

commit 254ffe45e5b5017e999d83c3d1f7994244d70ce1
parent aa66d17912149daae98f3093e027c92347614223
Author: Jared Tobin <jared@jtobin.ca>
Date:   Wed, 26 Jun 2013 09:29:26 +1200

Add list node swap exercise.

Diffstat:
A20130626_swaplistnodes/swap.hs | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)

diff --git a/20130626_swaplistnodes/swap.hs b/20130626_swaplistnodes/swap.hs @@ -0,0 +1,31 @@ +import Data.Foldable (toList) +import Data.Sequence (Seq) +import qualified Data.Sequence as Seq +import Test.QuickCheck + +swap :: Int -> [a] -> [a] +swap k xs = toList xsSwap + where + xsAsSeq = Seq.fromList xs + n = Seq.length xsAsSeq + xsSwap = let kth = xsAsSeq `Seq.index` k + nlkth = xsAsSeq `Seq.index` (n - k - 1) + in Seq.update (n - k - 1) kth . Seq.update k nlkth $ xsAsSeq + +-- Tests ----------------------------------------------------------------------- + +newtype Constrained = Constrained { getItems :: (Int, [Int]) } deriving Show + +instance Arbitrary Constrained where + arbitrary = do + NonEmpty xs <- arbitrary :: Gen (NonEmptyList Int) + let n = length xs + k <- choose (0, n - 1) + return $ Constrained (k, xs) + +prop_swapsKth :: Constrained -> Bool +prop_swapsKth (Constrained (k, xs)) = + let (swapped, n) = (swap k xs, length xs) + in xs !! k == swapped !! (n - k - 1) + && xs !! (n - k - 1) == swapped !! k +