I'm starting my graduate classes finally.  My school has this cool program called Fast Track where you're allowed to take graduate level classes as an undergrad student and get dual credit.  I finally qualified for the program and signed up for as many classes as I could fit in.

One of those classes is one called Advanced Programming Languages.  It's so advanced that I had to get permission from the Associate Dean.  He said no so I went to the teacher and he said he's out of town but it's ok with him as long as I learn OCaml before the class.  So I went back to the Associate Dean and he finally signed for my class.  Now I'm going through OCaml tutorials.  Having a strong background in procedural programming, it's very different for me.  I've done some work in the past with XSLT (a functional language written in xml, yuck) and used the functional parts of PHP, Ruby, and Python.  I've even tried to read the Neko compiler which is self hosting written in NekoML. NekoML is a variant of OCaml, but a little easier.  Well I couldn't read much of it.

Here is my first OCaml program.  It uses nested functions and takes advantage of the rec keyword to control function namespacing:

let range a b =
  let rec range a b accum =
    if b < a then accum
    else range a (b - 1) (b :: accum)
  in
  range a b [];;
 
range 1 10;;

Pretty cool huh.  I ran it in the interactive interpreter like I do in my scripting languages. The range function defined in the outer can't be used till after it's definition since I didn't include the rec keyword. The inner one uses rec and an extra accumulator so that it will be compiled with tail recursion.  Otherwise my range function would be very limited in what it could do without causing stack overflow.  Once the inner range function is defined, the outer one, which is just a wrapper, can call it directly.


1 Comment(s)

By: Mom on June 10, 2008

A blog is born!

Never even heard of OCaml before. Glad it is you instead of me taking that class.

Comments RSS TrackBack Identifier URI

Leave a comment