[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn] [x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]If the first list is not finite, the result is the first list.
>>> [1, 2, 3] ++ [4, 5, 6] [1,2,3,4,5,6]
>>> [] ++ [1, 2, 3] [1,2,3]
>>> [3, 2, 1] ++ [] [3,2,1]
mplus = (<|>)
handleInterrupt (outputStrLn "Ctrl+C was pressed, aborting!") someLongAction
>>> Just 2 *> Just 3 Just 3
>>> Nothing *> Just 3 NothingOf course a more interesting use case would be to have effectful computations instead of just returning pure values.
>>> import Data.Char >>> import GHC.Internal.Text.ParserCombinators.ReadP >>> let p = string "my name is " *> munch1 isAlpha <* eof >>> readP_to_S p "my name is Simon" [("Simon","")]
>>> "Hello World!" \\ "ell W" "Hoorld!"The second list must be finite, but the first may be infinite.
>>> take 5 ([0..] \\ [2..4]) [0,1,5,6,7]
>>> take 5 ([0..] \\ [2..]) * Hangs forever *
>>> "dog" `union` "cow" "dogcw"If equal elements are present in both lists, an element from the first list will be used. If the second list contains equal elements, only the first one will be retained:
>>> import Data.Semigroup(Arg(..)) >>> union [Arg () "dog"] [Arg () "cow"] [Arg () "dog"] >>> union [] [Arg () "dog", Arg () "cow"] [Arg () "dog"]However if the first list contains duplicates, so will the result:
>>> "coot" `union` "duck" "cootduk" >>> "duck" `union` "coot" "duckot"union is productive even if both arguments are infinite.
>>> [0, 2 ..] `union` [1, 3 ..] [0,2,4,6,8,10,12..
>>> [1,2,3,4] `intersect` [2,4,6,8] [2,4]If equal elements are present in both lists, an element from the first list will be used, and all duplicates from the second list quashed:
>>> import Data.Semigroup >>> intersect [Arg () "dog"] [Arg () "cow", Arg () "cat"] [Arg () "dog"]However if the first list contains duplicates, so will the result.
>>> "coot" `intersect` "heron" "oo" >>> "heron" `intersect` "coot" "o"If the second list is infinite, intersect either hangs or returns its first argument in full. Otherwise if the first list is infinite, intersect might be productive:
>>> intersect [100..] [0..] [100,101,102,103... >>> intersect [0] [1..] * Hangs forever * >>> intersect [1..] [0] * Hangs forever * >>> intersect (cycle [1..3]) [2] [2,2,2,2...