Functional Programming

Distance Course (2AD200)
in the Maths & Natural Sciences programme (MN)
at Uppsala University, Sweden

Tasks of Lab 0

Nothing has to be handed in about your activities in this lab.

Warm-up Exercises:

  1. What are the types of the following SML expressions:
  2. Check your answers with SML/NJ.
  3. What is wrong with each of the following SML expressions: If possible suggest appropriate corrections. Check your answers with SML/NJ.
  4. Give example values of the following SML types: Check your answers with SML/NJ.
  5. Implement the following specifications:
    cube x
    TYPE: real -> real
    PRE: (none)
    POST: x^3
    EXAMPLE: cube 2.0 = 8.0

    least (x,y,z)
    TYPE: int*int*int -> int
    PRE: (none)
    POST: the smallest of the numbers x, y, and z
    EXAMPLE: least (3,1,5) = 1

    third L
    TYPE: 'a list -> 'a
    PRE: L has at least three elements
    POST: the third element of L
    EXAMPLE: third ["This","is","an","example"] = "an"

    cycle L
    TYPE: 'a list -> 'a list
    PRE: (none)
    POST: [a2,a3,...,an,a1] when L=[a1,a2,...,an]
    EXAMPLE: cycle [1,2,3,4] = [2,3,4,1]

    pow i x
    TYPE: int -> real -> real
    PRE: i >= 0
    POST: x^i
    EXAMPLE: pow 3 2.0 = 8.0

    dup L
    TYPE: 'a list -> 'a list
    PRE: (none)
    POST: [a1,a1,a2,a2,...,an,an] when L=[a1,...,an]
    EXAMPLE: dup ["foo","bar"] = ["foo","foo","bar","bar"]

    len L
    TYPE: 'a list -> int
    PRE: (none)
    POST: the length of L
    EXAMPLE: len ["foo","bar","bar"] = 3

    largest L
    TYPE: int list -> int
    PRE: L is not empty
    POST: the largest element in L
    EXAMPLE: largest [-1,-3,-2] = -1

Last modified: Wed 20 Oct 17:45:42 MEST 2004