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
- 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). 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.
b) Running under Emacs
- Type emacs myprog.sml &. You might need to update your
~/.emacs file by adding lines from
our sample init.el file.
- Analyse the 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 system, select the
"switch to ML buffer" option from the menu. This should split your
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.
- Try and insert forms, etc. Become friendly with this
environment.
- If you do not come here often, install SML/NJ and emacs on
your home computer: the instructions and references are provided on the
course
homepage.
- Clicking the "tab" key indents the current line according to the SML conventions.
- C-h m provides you with documentation of the SML mode.
-
- C-h i provides you with documentation of Emacs (and other Unix programs).
- 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.
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:
- What are the types of the following SML expressions:
- (1.5,"3",7)
- fn x => x*x
- [1,2,3]
Check your answers with SML/NJ.
- What is wrong with each of the following SML expressions:
- #4 (1,2,3)
- 1+2.0
- 10/3
- -1
- [1,"hello"]
- [1,2.0]
If possible suggest appropriate corrections.
Check your answers with SML/NJ.
- Give example values of the following SML types:
- string * int
- real * (string * int)
- int * int -> int
- unit
- int -> int * int
- int -> int -> int
- int list list
Check your answers with SML/NJ.
-
Test run a program from the textbook or the lecture notes.
- 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