-- List concatenation defined by pattern matching: appendp [] ys = ys appendp (x:xs) ys = x : appendp xs ys --append (x:xs) ys = x : (append xs ys) -- with case expression: appendc xs ys = case xs of [] -> ys (x:xs) -> x : appendp xs ys -- List concatenation defined with selector functions: -- null xs: is xs the empty list? -- head xs: first element of xs -- tail xs: rest list of xs appends xs ys = if null xs then ys else head xs : appends (tail xs) ys -- Semantics of selector functions: equalities -- null [] == True -- null (x:xs) == False (for all x,xs) -- head (x:xs) == x -- tail (x:xs) == xs -- Example: as pattern mylast [x] = x -- last (_:(y:ys)) = last (y:ys) -- > construct a new list mylast (_ : xs@(_:_)) = mylast xs -- Boolean conjunction: and1 :: Bool -> Bool -> Bool and1 False False = False and1 False True = False and1 True False = False and1 True True = True loop :: Bool loop = loop and2 :: Bool -> Bool -> Bool and2 False _ = False and2 True b = b orp :: Bool -> Bool -> Bool orp True _ = True orp _ True = True orp False False = False -- sequential pattern matching: try rules from first to last, patterns from left to right