@@ -253,9 +253,11 @@ tail (Cons _ xs) = Just xs
253
253
-- |
254
254
-- | Running time: `O(n)`
255
255
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
259
261
260
262
-- | Break a list into its first element, and the remaining elements,
261
263
-- | or `Nothing` if the list is empty.
@@ -522,7 +524,7 @@ dropWhile p = go
522
524
-- | For example,
523
525
-- |
524
526
-- | ```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) }
526
528
-- | ```
527
529
-- |
528
530
-- | Running time: `O(n)`
@@ -646,9 +648,11 @@ intersectBy eq xs ys = filter (\x -> any (eq x) ys) xs
646
648
-- |
647
649
-- | Running time: `O(min(m, n))`
648
650
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
652
656
653
657
-- | A generalization of `zipWith` which accumulates results in some `Applicative`
654
658
-- | functor.
@@ -684,18 +688,24 @@ instance showList :: (Show a) => Show (List a) where
684
688
show (Cons x xs) = " Cons (" ++ show x ++ " ) (" ++ show xs ++ " )"
685
689
686
690
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
+
690
698
691
699
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
699
709
700
710
instance semigroupList :: Semigroup (List a ) where
701
711
append Nil ys = ys
@@ -705,8 +715,11 @@ instance monoidList :: Monoid (List a) where
705
715
mempty = Nil
706
716
707
717
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
+
710
723
711
724
instance foldableList :: Foldable List where
712
725
foldr _ b Nil = b
@@ -717,6 +730,7 @@ instance foldableList :: Foldable List where
717
730
go o b (Cons a as) = go o (b `o` a) as
718
731
foldMap f = foldl (\acc -> append acc <<< f) mempty
719
732
733
+
720
734
instance unfoldableList :: Unfoldable List where
721
735
unfoldr f b = go (f b)
722
736
where
0 commit comments