Latest update Oct 2, 2009.
To avoid spam, all mail addresses on this page have the "@" replaced by "#".
CD5100 Functional Programming
Assignment 1 - Getting started
1) Start up F# on your computer by starting Visual Studio and the F# Interactive Environment by selecting the View -> Other Windows -> F# Interactive menu item.
Enter a few arithmetic expressions at the prompt and press enter to evaluate them. Now try evaluating the expressions listed below:
3 :: [4; 5; 2; 7];;
List.length [4; 5; 2; 7];;
[4; 5; 2; 7] :: 3;;
The evaluation of the last expression generated errors. Why? What would you do to make it work?
2) You can only evaluate expressions that will fit on a single line at the prompt. If you want to write more complex expressions, or declare your own functions, you need to put the code in a file and then load that file into the F# Interactive Environment.
Create the file "Lab1.fs". You can use any text editor you like, but Visual Studio is recommended. Now declare some values in the file, for example:
#light
module Lab1
let x = 42
let myName = "Kalle"
let age = 25
let y = 4 + 2
The #light directive lightens up the syntax requirements. Save the file and load it into the system by writing: #load “c:\\...\\Lab1.fs”;;. You open the module by writing open “Lab1”;;. Then you can inspect the values by simply writing x;;, etc. The file and the module do not have to have the same name; however, it is convenient to do so.
Enter the following declarations into the file and evaluate. What values would b and c hold, and why?
let a = 5;;
let b = let a = 10 in a + 5;;
let c = a + b;;
3) Now it is time to declare some functions.
4) Define the function max2 that takes two integers as arguments and returns the largest of them and the function max_list that returns the largest of the elements in a nonempty list of integers by calling max2. For the empty list, it should abort with an error message (raising an exception).
Björn Lisper
bjorn.lisper#mdh.se
(Based on the 2002 version by Lars Bruce)