Lecture 02 Comparisons

Joseph Haugh

University of New Mexico

Java Sum

How do you sum the numbers from 1 to 10 in Java?

int s = 0;
for (int i = 0; i <= 10; i++) {
    s = s + i;
}

Haskell Sum

How do you sum the numbers from 1 to 10 in Haskell?

sum [1..10]

Haskell Preview

What does the following code do?

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]

Lists

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What do the square brackets ([]) mean?
  • They create lists or list comprehensions
  • What is a list then?
  • A collection of values of the same type.
  • For example: the list containing the numbers 1 2 3 is written: [1,2,3].
  • What does [] mean on its own?
  • It means that you want the empty list (no elements).
  • What does [1] mean?
  • It means you want a list of one element, 1.

Appending

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does the ++ do?
  • It is an infix function of two arguments
  • Both arguments must be lists
  • Both lists must be the same type
  • ++ then appends the two lists together

Appending

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • Appending example:

    [1,2,3] ++ [4,5,6] === [1,2,3,4,5,6]
  • What does this code do?

    [1,2,3] ++ [] === ??
    [] ++ [1,2,3] === ??

Appending

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • Appending example:

    [1,2,3] ++ [4,5,6] === [1,2,3,4,5,6]
  • What does this code do?

    [1,2,3] ++ [] === [1,2,3]
    [] ++ [1,2,3] === [1,2,3]

Consing

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does : do?

  • It is an infix function of two arguments

  • The first argument must be a value and the second a list of those values

  • For example:

    1 : [2,3,4] === [1,2,3,4]

Consing

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does : do?
  • It is an infix function of two arguments
  • The first argument must be a value and the second a list of those values
  • What does this code do?
1 : [2] === ??
1 : []  === ??

Consing

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does : do?
  • It is an infix function of two arguments
  • The first argument must be a value and the second a list of those values
  • What does this code do?
1 : [2] === [1,2]
1 : []  === [1]

Comprehensions

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does the  < − do?

  • Does the whole expression [a|a < −xs, a <  = x] remind of anything from math?

  • [ { a | a S a <= x  === [ a | a <- xs, a <= x ] ]}

  • For example:

    [x | x <- [1,2,3,4,5], x > 2] === [3,4,5]

Comprehensions

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does the  < − do?

  • Does the whole expression [a|a < −xs, a <  = x] remind of anything from math?

  • [ { a | a S a <= x  === [ a | a <- xs, a <= x ] ]}

  • What does this code do?

    [x | x <- [1,2,3,4,5], x > 4] === ??
    [x | x <- [1,2,3,4,5]]        === ??

Comprehensions

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • What does the  < − do?

  • Does the whole expression [a|a < −xs, a <  = x] remind of anything from math?

  • [ { a | a S a <= x  === [ a | a <- xs, a <= x ] ]}

  • What does this code do?

    [x | x <- [1,2,3,4,5], x > 4] === [5]
    [x | x <- [1,2,3,4,5]]        === [1,2,3,4,5]

Example

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]

What is f [3,1,4,2]?

What does this code represent?

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]

What does this code represent?

f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
  where
    ys = [ a | a <- xs, a <= x ]
    zs = [ b | b <- xs, b >  x ]
  • Quicksort!