Computer Science Department
Programming Methods Laboratory

Theorie des Langages /
Fondements de la Programmation
Ecole Polytechnique Federale de Lausanne
Exercises 1

To get started with Funnel you should copy the file /home/linuxsoft/funnel/lecture/demo.fn to your home account. It contains an implementation of the function sqrt defined in the lecture. You can test this function by invoking the funnel interpreter funni: funni demo.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 demo.fn to a bytecode file For executing the compiled binary, the Funnel virtual machine funnel has to be invoked: funnel demo.

After getting familiar with the environment you should start implementing complex numbers using records. 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, div, abs,
 conjugate, toString)]'
> t.toString
"(19, 17)"
> t.div(makeComplex(5, 2)).times(makeComplex(5, 2)).toString
"(19.000002, 17.0)"
> t.abs
> sqrt(t.times(t.conjugate(t)).real)