@@ -1,51 +1,76 @@
1−
21module Either where
32
3+{-| Represents any data that can take two different types.
4+
5+# Type and Constructors
6+@docs Either, Left, Right
7+
8+# Taking Eithers apart
9+@docs either, isLeft, isRight
10+
11+# Eithers and Lists
12+@docs lefts, rights, partition, consLeft, consRight, consEither
13+
14+-}
15+
416import List
517
6−-- Represents any data that can take two different types.
7−--
8−-- This can also be used for error handling `(Either String a)` where error
9−-- messages are stored on the left, and the correct values (“right”
10−-- values) are stored on the right.
18+{-| This can also be used for error handling `(Either String a)` where
19+ error messages are stored on the left, and the correct values
20+ (“right” values) are stored on the right.
21+-}
1122data Either a b = Left a | Right b
1223
13−-- Apply the first function to a `Left` and the second function to a `Right`.
14−-- This allows the extraction of a value from an `Either`.
24+{-| Apply the first function to a `Left` and the second function to a `Right`.
25+ This allows the extraction of a value from an `Either`.
26+-}
1527either : (a -> c) -> (b -> c) -> Either a b -> c
1628either f g e = case e of { Left x -> f x ; Right y -> g y }
1729
18−-- True if the value is a `Left`.
30+{-| True if the value is a `Left`. -}
1931isLeft : Either a b -> Bool
2032isLeft e = case e of { Left _ -> True ; _ -> False }
2133
22−-- True if the value is a `Right`.
34+{-| True if the value is a `Right`. -}
2335isRight : Either a b -> Bool
2436isRight e = case e of { Right _ -> True ; _ -> False }
2537
26−-- Keep only the values held in `Left` values.
38+{-| Keep only the values held in `Left` values. -}
2739lefts : [Either a b] -> [a]
2840lefts es = List.foldr consLeft [] es
2941
30−-- Keep only the values held in `Right` values.
42+{-| Keep only the values held in `Right` values. -}
3143rights : [Either a b] -> [b]
3244rights es = List.foldr consRight [] es
3345
34−-- Split into two lists, lefts on the left and rights on the right. So we
35−-- have the equivalence: `(partition es == (lefts es, rights es))`
46+{-| Split into two lists, lefts on the left and rights on the right. So we
47+ have the equivalence: `(partition es == (lefts es, rights es))`
48+-}
3649partition : [Either a b] -> ([a],[b])
3750partition es = List.foldr consEither ([],[]) es
3851
52+{-| If `Left`, add the value to the front of the list.
53+ If `Right`, return the list unchanged
54+-}
55+consLeft : Either a b -> [a] -> [a]
3956consLeft e vs =
4057 case e of
4158 Left v -> v::vs
4259 Right _ -> vs
4360
61+{-| If `Right`, add the value to the front of the list.
62+ If `Left`, return the list unchanged.
63+-}
64+consRight : Either a b -> [b] -> [b]
4465consRight e vs =
4566 case e of
4667 Left _ -> vs
4768 Right v -> v::vs
4869
70+{-| If `Left`, add the value to the left list.
71+ If `Right`, add the value to the right list.
72+-}
73+consEither : Either a b -> ([a], [b]) -> ([a], [b])
4974consEither e (ls,rs) =
5075 case e of
5176 Left l -> (l::ls,rs)