23. cons :: a -> [a] -> [a] --
cons x [] = [x] --
cons x1 (x2:xs) = x1 : (cons x2 xs)
// C++
template <typename a>
std::list<a>
cons(a const& x, std::list<a> const& xs);
Monday, June 13, 2011 23
24. cons :: a -> [a] -> [a] --
cons x [] = [x] --
cons x1 (x2:xs) = x1 : (cons x2 xs)
e.g. cons 0 [1,2,3,4]
=> x1 0, x2 1, xs [2,3,4]
Monday, June 13, 2011 24
25. (Int)
sum
sum :: [Int] -> Int
sum [] = 0
sum (n:ns) = n + (sum ns)
Monday, June 13, 2011 25
26. cons sum
cons :: a -> [a] -> [a]
cons x [] = [x]
cons x1 (x2:xs) = x1 : (cons x2 xs)
sum :: [Int] -> Int
sum [] = 0
sum (n:ns) = n + (sum ns)
Monday, June 13, 2011 26
27. cons x [] = [x]
sum [] = 0
cons x1 (x2:xs) = x1 : (cons x2 xs)
sum (n :ns) = n + (sum ns)
Monday, June 13, 2011 27
28. (Int)
product
product :: [Int] -> Int
product [] = 1
product (n:ns) = n * product ns
Monday, June 13, 2011 28
29. cons x [] = [x]
sum [] = 0
product [] = 1
cons x1 (x2:xs) = x1 : (cons x2 xs)
sum (n :ns) = n + (sum ns)
product (n :ns) = n * (product ns)
Monday, June 13, 2011 29