Version française
Home     About     Download     Resources     Contact us    
Browse thread
Before teaching OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Before teaching OCaml
On Sunday 07 January 2007 18:15, David Teller wrote:
>   Dear list,
>
>  I'm going to start teaching OCaml soon and I'm fishing for ideas and
> suggestions. I hope this list is the right place to ask.
>
>  Within a few weeks, I'll be teaching OCaml to a class of second-year
> students in _mathematics & informatics_.

We have created quite a bit of free educational material about OCaml/F# and 
aimed broadly at scientists. I'm sure you'll get some inspiration from our 
work.

> The bad part is that their 
> knowledge of computer science is limited to 3 term-long lectures of
> "algorithmics" (read "Java under Windows"),

That isn't good but mathematicians should be able to learn the basics of 
asymptotic algorithmic complexity in an hour. My book "OCaml for Scientists" 
contains an ideal mathematical explanation of this topic, using ipow and a 
case study and plotting the real and asymptotic complexities as well as the 
measured time taken.

> and that they have nil 
> knowledge of Unix/Cygwin or Makefiles, or even Emacs or command-lines.

I use Emacs under Debian linux. The syntax highlighting (of mli, ml but also 
mll and mly files) is great and autoindentation is indispensable for me. 
However, there is a bit of a learning curve here, especially if they have to 
install the software themselves. Also, I'm not sure how well it works under 
Windows.

If you're using Windows then I highly recommend the related language F# and 
Visual Studio 2005. The IDE does color syntax highlighting for .fs and .fsi 
(equivalent to .ml and .mli) files. As a .NET language, you get instant 
access to all of the functionality provided by .NET, such as database and web 
connectivity.

> The good part is that a number of them consider Java "not mathematical
> enough", so they may be good candidates for functional programming.

Excellent. There are many programs that they will probably jump at. You might 
like to start with a logic problem, like solving Sudoku puzzles:

  http://www.ffconsultancy.com/free/sudoku/

The core of the program is simply this:

let rec search ?(x=0) ?(y=0) f accu = match x, y with
    9, y -> search ~x:0 ~y:(y+1) f accu (* Next row *)
  | 0, 9 -> f accu
  | x, y ->
      if m.(y).[x] <> '0' then search ~x:(x+1) ~y f accu else
        fold (fun accu n ->
                let n = Char.chr (n + 48) in
                if invalid x y n then accu else
                  (m.(y).[x] <- n;
                   let accu = search ~x:(x+1) ~y f accu in
                   m.(y).[x] <- '0';
                   accu)) accu 1 10

We've also written a GUI version in F# using Windows Forms, threads and other 
features:

  http://www.ffconsultancy.com/dotnet/fsharp/sudoku/

>  I'm planning to base my lecture roughly on part 1 of _Developing
> applications with Objective Caml_,

If you're interested in teaching functional programming more than other 
aspects (e.g. interoperability) then you might also consider the freely 
available first chapter of my book:

  http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html

> perhaps replacing the chapter devoted 
> to Graphics with the use of LablGTK.

I recommend using OpenGL with OCaml/Linux or DirectX with Windows.

The OpenGL examples from my book are freely available here:

  http://www.ffconsultancy.com/products/ocaml_for_scientists/visualisation/

The minimal demo is just this:

let () =
  let argv' = Glut.init Sys.argv in
  ignore (Glut.createWindow ~title:"OpenGL Demo");
  GlClear.color (0.1, 0.3, 0.1);
  Glut.displayFunc ~cb:(fun () -> GlClear.clear [ `color ]; Gl.flush ());
  Glut.mainLoop ()

Here is a DirectX demo for F#:

  http://www.ffconsultancy.com/dotnet/fsharp/teapot/

I have just finished writing a minimal scene graph library for F# for my 
forthcoming book "F# for Scientists". This defines a variant type 
representing nested transforms, groups and primitives. When the top-level 
(called the F# interactive mode) sees one of these it automatically spawns a 
visualisation window. This is ideal for educational purposes as the students 
are freed from the burden of rendering contexts, device losses, double 
buffering and so on.

> Then again, perhaps not. Some 
> low-level graphics might be interesting for them.

Creating graphics with OpenGL and DirectX is easy enough that the immense 
gratification given by the result make it worthwhile. I strongly recommend 
using graphics on a teaching course.

> I also intend to give 
> them a term-long project to work on and develop.

As other people have suggested, some form of interpreter or compiler is ideal 
here. I think such a project would be greatly improved with the addition of 
graphics commands into the language. So a Logo, BASIC or Mathematica 
implementation would be ideal.

Given that they are mathematicians, perhaps a mini Mathematica implementation 
is an ideal project? This has the interesting property of being a rewrite 
system (expr -> expr) rather than a conventional interpreter (expr -> value). 
A Mathematica implementation simply replaces input expressions according to a 
built-in set of rewrite rules (pattern matches). A simple Mathematica 
implementation can be written in well under 1,000LOC, including lexer and 
parser.

> Right now, I see the following difficulties:
>
> * the environment -- under Windows, is there any viable alternative to
> Emacs + the MinGW-based port ?

The commercial version of Visual Studio is the only other IDE that I know of 
under Windows.

> * the Makefile -- I've found OCamlMakefile [1] but I haven't tried it
> yet, hopefully it's simple enough for my students to use without too
> many arcane manipulations

Make is really only useful for final, production builds in the context of 
OCaml. OCaml is very brittle with respect to changes but it is very fast to 
compile, so it is an ideal candidate for simple compilation from a bash 
script.

> * the task -- for the moment, I have no interesting idea of OCaml-based
> projects. Perhaps something like finding the shortest path along
> subway/train lines ?

The travelling salesman problem is one of the examples from the final chapter 
of my book:

  http://www.ffconsultancy.com/products/ocaml_for_scientists/complete/

It would be much more compelling with real-time graphics. I'll address that in 
my next book...

We also have many other OCaml examples, including a minimal Mandelbrot 
renderer:

  http://www.ffconsultancy.com/free/fractal/

A simple ray tracer:

  http://www.ffconsultancy.com/free/ray_tracer/languages.html

A maze generator:

  http://www.ffconsultancy.com/free/maze/

and so on.

HTH!

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists