Functional Programming MN1

Distance Course (1dl027)
Uppsala University, Sweden

Tasks of Lab 0

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

Editing and running SML programs

You can either a) run SML directly, from the Unix command line, or b) run SML under Emacs. You are recommended to explore both options.

a) Running directly

b) Running under Emacs

Solve Warm-up Exercises:

After you get familiar with editing and executing SML code in your favourite environment, practice basic SML programming by solving the warm-up exercises below.

Do not despair if you cannot yet do everything. Do as much as you can. We understand that you may have different backgrounds and preliminary knowledge. Sometimes you have difficulties to start. We have seen many students that made a tremendous breakthrough from zero knowledge to mastering and loving FP in a short period of time.

If, on the other hand, you do not feel the excercises are difficult enough for you, try and solve your own problem/puzzle using SML, or ask us to give you a problem to try.

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. Test run a program from the textbook or the lecture notes.
  6. 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

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

    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"

    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: Thu Nov 8 11:41:30 MET 2007