diff --git a/src/Data/List.purs b/src/Data/List.purs index d575c84..afc2160 100644 --- a/src/Data/List.purs +++ b/src/Data/List.purs @@ -517,7 +517,7 @@ slice start end xs = take (end - start) (drop start xs) take :: forall a. Int -> List a -> List a take = go Nil where - go acc 0 _ = reverse acc + go acc n _ | n < 1 = reverse acc go acc _ Nil = reverse acc go acc n (x : xs) = go (x : acc) (n - 1) xs @@ -541,7 +541,7 @@ takeWhile p = go Nil -- | -- | Running time: `O(n)` where `n` is the number of elements to drop. drop :: forall a. Int -> List a -> List a -drop 0 xs = xs +drop n xs | n < 1 = xs drop _ Nil = Nil drop n (x : xs) = drop (n - 1) xs diff --git a/test/Test/Data/List.purs b/test/Test/Data/List.purs index f476dc3..42d84c4 100644 --- a/test/Test/Data/List.purs +++ b/test/Test/Data/List.purs @@ -225,21 +225,25 @@ testList = do assert $ (take 1 (l [1, 2, 3])) == l [1] assert $ (take 2 (l [1, 2, 3])) == l [1, 2] assert $ (take 1 nil) == nil + assert $ (take 0 (l [1, 2])) == l [] + assert $ (take (-1) (l [1, 2])) == l [] log "takeEnd should keep the specified number of items from the end of an list, discarding the rest" assert $ (takeEnd 1 (l [1, 2, 3])) == l [3] assert $ (takeEnd 2 (l [1, 2, 3])) == l [2, 3] assert $ (takeEnd 1 nil) == nil + assert $ (takeEnd 2 (l [1])) == l [1] log "takeWhile should keep all values that match a predicate from the front of an list" assert $ (takeWhile (_ /= 2) (l [1, 2, 3])) == l [1] assert $ (takeWhile (_ /= 3) (l [1, 2, 3])) == l [1, 2] assert $ (takeWhile (_ /= 1) nil) == nil - log "dropE should remove the specified number of items from the front of an list" + log "drop should remove the specified number of items from the front of an list" assert $ (drop 1 (l [1, 2, 3])) == l [2, 3] assert $ (drop 2 (l [1, 2, 3])) == l [3] assert $ (drop 1 nil) == nil + assert $ (drop (-1) (l [1, 2, 3])) == l [1, 2, 3] log "dropEnd should remove the specified number of items from the end of an list" assert $ (dropEnd 1 (l [1, 2, 3])) == l [1, 2]