-- Comp1100 Sem 1 2006
-- Patterns and Higher-Order Functions
-- Examples for lecture
-- Clem Baker-Finch March 2006

module MapDemo where

import Prelude hiding (map)
import Data.Char  -- for toUpper

-- Examples showing common pattern --

-- Double every element of a list of Ints

double :: [Int] -> [Int]
double []      = []
double (x:xs)  = 2*x : double xs

-- Increment every element of a list of Ints

incr :: [Int] -> [Int]
incr []      = []
incr (x:xs)  = x+1 : incr xs

-- Convert all characters of a string to upper case
-- Function  toUpper :: Char -> Char  is from Data.Char library

upperCase :: [Char] -> [Char]
upperCase []      = []
upperCase (x:xs)  = toUpper x : upperCase xs

-- The standard definition of map --

map :: (a -> b) -> [a] -> [b]
map f []      = []
map f (x:xs)  = f x : map f xs

-- Equivalent definitions using map

double' xs    = map (2*) xs

incr' xs      = map (+1) xs

upperCase' xs = map toUpper xs

-- In fact there is no need for xs argument in definition

double''    = map (2*)

incr''      = map (+1)

upperCase'' = map toUpper


