# Functional Programming

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

Nothing has to be handed in about your activities in this lab.
• Start SML/NJ in interactive mode: enter sml to the Unix prompt and see what happens. Use Ctrl-C to break the current computation, and Ctrl-D to leave SML/NJ.
• Try and evaluate several simple SML expressions (one- and multi-line). Try and give erroneous inputs, and analyse the replies. You can start with the warm-up exercises 1 to 3 below.
• Become comfortable with the interactive mode.
• Learn what use "foo.sml" ; serves for.
• Now you are ready to edit and save SML programs in your favourite editor: run and debug them in SML/NJ.
• A much better way to proceed is as follows:
• Type xemacs myprog.sml & and see what happens. Try also emacs myprog.sml &. You might need to update your ~/.emacs or ~/.xemacs/init.el file by adding lines from our sample init.el file.
• Analyse the [x]emacs toolbar: do you notice anything SML-related?
• Investigate the SML menu and learn how to start SML/NJ.
• After you have started the SML interpreter, select the "switch to ML buffer" option from the menu. This should split your [x]emacs window in two: in one you edit your program and send it to the other, in which you run SML/NJ interactively. This allows convenient and efficient SML programming.
• Inside the SML/NJ interpreter window, you can use Ctrl-UPARROW or Alt-P to re-enter the last typed expression.
• Learn and use as much as you can about the options provided in the SML menu.
• For example, type fun factorial 0 = 1 in myprog.sml and then try electric pipe. You will enjoy how comfortable programming becomes.
• Try and insert forms, etc. Become friendly with this environment.
• If you do not come here often, install SML/NJ and [x]emacs on your home computer: the instructions and references are provided on the course homepage.
• Spend as much time as you can trying to understand and practice functional programming. First it may seem difficult, but your efforts will be quickly and generously rewarded. Functional programming is fun and elegant: you will like it.
• After you get familiar with editing and executing SML code in your favourite environment, practice basic SML programming by solving the warm-up exercise 4 below.
• Proceed to Assignment 1. 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 assignments are difficult enough for you, try and solve your own problem/puzzle using SML, or ask us to give you a problem to try. Usually, well-known small problems, like 8 queens, knight's tour, or sorting give you good opportunities to compare FP with imperative programming, and get a real impression of the advantages of FP, in both elegance and efficiency.
• Have fun!

### Warm-up Exercises:

1. What are the types of the following SML expressions:
• (1.5,("3",[4,5]))
• [[1,2],nil,[3]]
• [(2,3.5),(4,5.5),(6,7.5)]
2. What is wrong with each of the following SML expressions:
• #4 (1,2,3)
• hd([])
• ["r"]::["a","t"]
• tl([])
• 1 @ 2
3. Give example values of the following SML types:
• int list list list
• (int * char) list
• string list * (int * (real * string)) * int
• ((int*int) * (bool list) * real) * (real * string)
4. 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