Skip to content

Commit 37fd9d6

Browse files
committed
made some functions tailrec
1 parent 28f3eec commit 37fd9d6

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

docs/Data/List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ Split a list into two parts:
486486
For example,
487487

488488
```purescript
489-
span (\n -> n % 2 == 1) (1 : 3 : 2 : 4 : 5 : Nil) == Tuple (1 : 3 : Nil) (2 : 4 : 5 : Nil)
489+
span (\n -> n % 2 == 1) (1 : 3 : 2 : 4 : 5 : Nil) == { init: (1 : 3 : Nil), rest: (2 : 4 : 5 : Nil) }
490490
```
491491

492492
Running time: `O(n)`

src/Data/List.purs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,11 @@ tail (Cons _ xs) = Just xs
253253
-- |
254254
-- | Running time: `O(n)`
255255
init :: forall a. List a -> Maybe (List a)
256-
init (Cons x Nil) = Just Nil
257-
init (Cons x xs) = Cons x <$> init xs
258-
init _ = Nothing
256+
init Nil = Nothing
257+
init lst = Just $ reverse $ go lst Nil
258+
where
259+
go (Cons x Nil) acc = acc
260+
go (Cons x xs) acc = go xs $ Cons x acc
259261

260262
-- | Break a list into its first element, and the remaining elements,
261263
-- | or `Nothing` if the list is empty.
@@ -522,7 +524,7 @@ dropWhile p = go
522524
-- | For example,
523525
-- |
524526
-- | ```purescript
525-
-- | span (\n -> n % 2 == 1) (1 : 3 : 2 : 4 : 5 : Nil) == Tuple (1 : 3 : Nil) (2 : 4 : 5 : Nil)
527+
-- | span (\n -> n % 2 == 1) (1 : 3 : 2 : 4 : 5 : Nil) == { init: (1 : 3 : Nil), rest: (2 : 4 : 5 : Nil) }
526528
-- | ```
527529
-- |
528530
-- | Running time: `O(n)`
@@ -646,9 +648,11 @@ intersectBy eq xs ys = filter (\x -> any (eq x) ys) xs
646648
-- |
647649
-- | Running time: `O(min(m, n))`
648650
zipWith :: forall a b c. (a -> b -> c) -> List a -> List b -> List c
649-
zipWith _ Nil _ = Nil
650-
zipWith _ _ Nil = Nil
651-
zipWith f (Cons a as) (Cons b bs) = Cons (f a b) (zipWith f as bs)
651+
zipWith f xs ys = reverse $ go xs ys Nil
652+
where
653+
go Nil _ acc = acc
654+
go _ Nil acc = acc
655+
go (Cons a as) (Cons b bs) acc = go as bs $ Cons (f a b) acc
652656

653657
-- | A generalization of `zipWith` which accumulates results in some `Applicative`
654658
-- | functor.
@@ -684,18 +688,24 @@ instance showList :: (Show a) => Show (List a) where
684688
show (Cons x xs) = "Cons (" ++ show x ++ ") (" ++ show xs ++ ")"
685689

686690
instance eqList :: (Eq a) => Eq (List a) where
687-
eq Nil Nil = true
688-
eq (Cons x xs) (Cons y ys) = x == y && xs == ys
689-
eq _ _ = false
691+
eq xs ys = go xs ys true
692+
where
693+
go _ _ false = false
694+
go Nil Nil acc = acc
695+
go (Cons x xs) (Cons y ys) acc = go xs ys $ acc && (y == x)
696+
go _ _ _ = false
697+
690698

691699
instance ordList :: (Ord a) => Ord (List a) where
692-
compare Nil Nil = EQ
693-
compare Nil _ = LT
694-
compare _ Nil = GT
695-
compare (Cons x xs) (Cons y ys) =
696-
case compare x y of
697-
EQ -> compare xs ys
698-
other -> other
700+
compare xs ys = go xs ys
701+
where
702+
go Nil Nil = EQ
703+
go Nil _ = LT
704+
go _ Nil = GT
705+
go (Cons x xs) (Cons y ys) =
706+
case compare x y of
707+
EQ -> go xs ys
708+
other -> other
699709

700710
instance semigroupList :: Semigroup (List a) where
701711
append Nil ys = ys
@@ -705,8 +715,11 @@ instance monoidList :: Monoid (List a) where
705715
mempty = Nil
706716

707717
instance functorList :: Functor List where
708-
map _ Nil = Nil
709-
map f (Cons x xs) = Cons (f x) (f <$> xs)
718+
map f lst = reverse $ go lst Nil
719+
where
720+
go Nil acc = acc
721+
go (Cons x xs) acc = go xs $ Cons (f x) acc
722+
710723

711724
instance foldableList :: Foldable List where
712725
foldr _ b Nil = b
@@ -717,6 +730,7 @@ instance foldableList :: Foldable List where
717730
go o b (Cons a as) = go o (b `o` a) as
718731
foldMap f = foldl (\acc -> append acc <<< f) mempty
719732

733+
720734
instance unfoldableList :: Unfoldable List where
721735
unfoldr f b = go (f b)
722736
where

0 commit comments

Comments
 (0)