Computer Science Department
Programming Methods Laboratory

Foundations of Programming
Ecole Polytechnique Federale de Lausanne
Exercises 1

To get started with Funnel you should copy the file /home/linuxsoft/funnel/lecture/intro.fn to your home account. It contains an implementation of the function sqrt defined in the first lecture. You can test this function by invoking the funnel interpreter funni with the -untyped option: funni -untyped intro.fn. You enter an interpreter loop which accepts Funnel expressions as input, evaluates these expressions and prints out the result of the evaluation. You can exit the interpreter by entering :q. Command :h gives you an overview over all interpreter commands.

Alternatively to the interpreter you can use the Funnel compiler funnelc directly. It compiles a Funnel source file intro.fn to a bytecode file Do not forget to specify the -untyped option again when compiling dynamically typed Funnel programs. For executing the compiled binary, the Funnel virtual machine funnel has to be invoked: funnel intro.

After getting familiar with the environment you should start implementing complex numbers using records. For this task you can copy a framework as well from /home/linuxsoft/funnel/lecture/complex.fn. Your complex number datatype should support the functions real, imag, plus, minus, times, div, abs, conjugate and toString:

  • real and imag return the real or imaginary part of the complex number,
  • plus, minus, times and div implement basic arithmetic operations,
  • abs returns the absolut value of the complex number,
  • conjugate calculates the complex conjugate, and
  • toString returns a string representation of the complex number. Like in Java, you can use the + operator for string concatenation if one of the operands is a string. Operands not beeing a string are automatically converted into a string.
Here is a transcript of a funni session in which the complex datatype is used:
> val I = makeComplex(0, 1)
'val I = [record id=1, adr=0, type=(real, imag, plus, minus, times, div,
                                    abs, conjugate, toString)]'
> I.times(I).toString
"(-1, 0)"
> val t = makeComplex(4, 7).times(makeComplex(3, -1))
'val t = [record id=1, adr=0, type=(real, imag, plus, minus, times,...]'
> t.toString
"(19, 17)"
> t.div(makeComplex(5, 2)).times(makeComplex(5, 2)).toString
"(19.000002, 17.0)"
> sqrt(t.times(t.conjugate(t)).real)