From 7b2e5d676e2083fbf6cb4e128b58a1d84cacf81c Mon Sep 17 00:00:00 2001 From: Jeffrey Rosenbluth Date: Fri, 18 Jul 2014 12:28:10 -0400 Subject: [PATCH] Simplify some functions in ListZipper --- src/Course/ListZipper.hs | 47 ++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/Course/ListZipper.hs b/src/Course/ListZipper.hs index aa466b985..60887ed82 100644 --- a/src/Course/ListZipper.hs +++ b/src/Course/ListZipper.hs @@ -453,15 +453,11 @@ moveLeftN' :: Int -> ListZipper a -> Either Int (ListZipper a) -moveLeftN' n z = - let moveLeftN'' n' z' q - | n' == 0 = Right z' - | n' < 0 = moveRightN' (negate n') z - | otherwise = - case moveLeft z' of - IsZ zz -> moveLeftN'' (n' - 1) zz (q + 1) - IsNotZ -> Left q - in moveLeftN'' n z 0 +moveLeftN' n z + | n < 0 = moveRightN' (negate n) z + | otherwise = case moveLeftN n z of + IsZ zz -> Right zz + IsNotZ -> Left $ length (lefts z) -- | Move the focus right the given number of positions. If the value is negative, move left instead. -- If the focus cannot be moved, the given number of times, return the value by which it can be moved instead. @@ -484,15 +480,11 @@ moveRightN' :: Int -> ListZipper a -> Either Int (ListZipper a) -moveRightN' n z = - let moveRightN'' n' z' q - | n' == 0 = Right z' - | n' < 0 = moveLeftN' (negate n') z - | otherwise = - case moveRight z' of - IsZ zz -> moveRightN'' (n' - 1) zz (q + 1) - IsNotZ -> Left q - in moveRightN'' n z 0 +moveRightN' n z + | n < 0 = moveLeftN' (negate n) z + | otherwise = case moveRightN n z of + IsZ zz -> Right zz + IsNotZ -> Left $ length (rights z) -- | Move the focus to the given absolute position in the zipper. Traverse the zipper only to the extent required. -- @@ -508,14 +500,13 @@ nth :: Int -> ListZipper a -> MaybeListZipper a -nth i z = - if i < 0 - then - IsNotZ - else - case moveLeftN' i z of - Left a -> moveRightN (i-a) z - Right (ListZipper l _ _) -> moveLeftN (length l) z +nth n z + | n < 0 = IsNotZ + | v == 0 = IsZ z + | v < 0 = moveLeftN (negate v) z + | otherwise = moveRightN v z + where + v = n - index z -- | Return the absolute position of the current focus in the zipper. -- @@ -526,8 +517,8 @@ nth i z = index :: ListZipper a -> Int -index (ListZipper l _ _) = - length l +index = length . lefts + -- | Move the focus to the end of the zipper. --