Change the swap function so its Haskell 98, by using list-comps instead of pattern-guards.

This commit is contained in:
Neil Mitchell
2007-05-08 12:31:58 +00:00
parent 9f4fd822b6
commit 27f1f50071

View File

@@ -1,5 +1,3 @@
{-# OPTIONS -fglasgow-exts #-}
-----------------------------------------------------------------------------
-- |
-- Module : StackSet
@@ -8,7 +6,7 @@
--
-- Maintainer : dons@cse.unsw.edu.au
-- Stability : stable
-- Portability : portable, needs GHC 6.6
-- Portability : portable
--
-----------------------------------------------------------------------------
--
@@ -201,12 +199,11 @@ promote w = maybe w id $ do
-- > swap a b . swap a b == id
--
swap :: Eq a => a -> a -> [a] -> [a]
swap a b xs | a == b = xs -- do nothing
| Just ai <- L.elemIndex a xs
, Just bi <- L.elemIndex b xs = insertAt bi a (insertAt ai b xs)
swap a b xs = head $ [insertAt bi a (insertAt ai b xs) | a /= b
,Just ai <- [L.elemIndex a xs], Just bi <- [L.elemIndex b xs]]
++ [xs]
where insertAt n x ys = as ++ x : tail bs
where (as,bs) = splitAt n ys
swap _ _ xs = xs -- do nothing
--
-- cycling: