Skip to main content

Teaching Programming Languages with Scala

Posted by cayhorstmann on September 12, 2008 at 6:29 AM PDT

This semester, I am teaching the undergraduate programming languages course
at SJSU, a required course for CS majors. The course has two objectives:

  • To give students significant exposure to a programming paradigm other
    than OO
  • To make students understand the process of translating a high-level
    language into machine (or VM) code

The canonical way to meet these objectives is to teach Scheme, and to build
simple interpreters and compilers. It is not the most popular course. Most of
our students just don't think Scheme is all that cool. When I want them to
admire the elegance of closures and continuations, they are still hung up over
parentheses and let.

style="float: left; margin-right: 1em;" />This semester, I am trying something
new. I am using Scala as my functional language. The syntax is accessible to
the students (they all know Java), and they see Scala mentioned on href="http://dzone.com">dzone.com, so it isn't just something of interest
to a bunch of pointy-headed professors.

In the first few weeks, I am restricting myself to the functional subset of
the language:

  • val, not var
  • No while, just recursion
  • Immutable lists, just with head, tail, and
    ::
  • The higher-order functions in the List class:
    map, filter, /:, etc.

I can do exactly the same as in Scheme, except without the parentheses. Does
it work? I had a few lectures, and here are my initial observations.

  • The Scala interpreter is great. Students enjoy the immediate
    feedback.
  • style="float: right; margin-left: 1em;" />Students need to install the JDK and Scala on their laptops. The worst
    part was setting up Java on Windows. If the installer must put the JDK into
    Program Files, couldn't it at least add the bin
    directory to the PATH? It requires superhuman effort to get
    the less talented students to edit the system configuration or to deal with
    spaces in file names in the command shell.
  • The DOS shell still sucks eggs,
    just as it has ever since I first saw it in Windows 3.0. The better
    students run Linux, Mac OS, or href="http://freemode.net/archives/000121.html">Cygwin with rxvt.
    Microsoft sends a powerful signal to budding computer scientists. That
    signal is: "We don't give a !@#$ about you!".
  • The biggest hangup so far has been the strong typing, somewhat to my
    surprise. x : Int instead of int x, but one
    quickly gets used to that. However, typing
    append(lst : List[Any], a : Any) : List[Any]
    into an interpreter window is no great joy. I expect this will get
    better when we move to Eclipse, where the IDE can take advantage of the
    strong typing.

/>Interested in following along? Click on the links of href="http://horstmann.com/sjsu/cs152/schedule.html">this schedule. There
are lab assignments in each lecture. If you need a hint with one of the labs,
leave a blog comment.

Comments

I'll take a look at lectures, normally I do find though that lectures at universities are not so interesting ... just a personal "mind-set". You know what Cay? sometimes we oughtta wipe the slate clean and say bye bye to one language (I blv James Gosling said the same thing?) ; introduce and work with something that is just better. I am talking about Java and how they bungled it with generics (non runtime syntactic sugar), closures and fp friendly constructs ... and now more recently with series of abandoned JSRs (JAM JSR for example). Scala is definitely very powerful and better language than Java and i do hope that programmers realize it before it becomes another language that only some of us know and understand and love. Linus Torvalds was kind hitting on C++ saying that it has STL that will allow us to do things very easily and though in part he was critiquing performance his partial idea that very often crappy programmers come and write crappy code as part of their job and they get the job because c++ makes it easier for them ... reminds me of the same facts here? I am not trying to be blunt, but i think lot of people just take up a programming language because hey they are required to; and that they are not really programmers by trait, when that happens you get people who dislike change for better or for worse; Just my 2 cents, I don't mean any disrespect to anyone in particular. Regards Vyas, Anirudh

Does the Scala interpreter work when running in rxvt on your system? If I enter an expression and hit return it just hangs and never prints the result.

rxvt woes

Andrew Beacock has posted a solution to the problem with scala hanging in rxvt under cygwin: https://www.blogger.com/comment.g?blogID=11593374&postID=142812913044950...

Jobs? Scala jobs will follow. Its too young to have jobs. I don't know if Scala will take over the world, but I do know that Java is not the solution for when we have 32 processors per chip. So, yeah, I want to take the course.! Oat

JohnR: The setup problem is with Java (!), not Scala. Scala is perfectly easy to install. The JDK installer should (a) add its bin directory to the global PATH and (b) check whether some other app set the global CLASSPATH (such Apple's Quicktime) and then add "." to it. But I suppose I could make a thumb drive or a VMWare image with Java + Scala on it. vinay_maddi: You can follow the lectures online (using the link provided), or if you are local, you can show up (with a laptop) at the classroom (MH 225, TR1330-1445).

Is there anyway I could attend the class without registering for the course.

Bravo Cay! Programming concepts are programming concepts regardless of the language that you use... so why not pick a language that the kids think is "sexy" (or at least relevant)? I applaud your efforts, and hope that folks who know a heck of a lot more about Scala then I do will respond and give you tips on how to make it easier for your kids to get set up. Ignorant as I am... Is it possible to run Scala from a thumb drive? -JohnR

If you want the students to focus more on the language and less on the setup, consider distributing a virtual machine image to them pre-configured with Linux, Java, and, Scala already installed. This gives them a nice sandbox and some exposure to Linux, and virtualization concepts. Many VM products like Virtual Box and VMWare Player cost nothing and are easy to set up in Windows. On the other hand, there's probably something to be said for going through the pain of setting up a development environment. And older laptops might have a hard time running a VM.

If your course is about science (the thinkers), teach scheme or whatever other language required to force students to think about the programming model, paradigms, etc. but if your course is not-pure-science-oriented and the goal is to produce technicians with some background in formal knowledge, always choose a market accepted technology to teach the students.. in that way, even if they like or not, they will finish the semester with something useful in hands :) I am not sure about the presence of Scala in the real world ( I mean number of jobs out there ), but for sure it is a nice language and an excellent learning prototype.....

ok, very nice then.. (btw, TIOBE ranks Scala with 0.09% of the market.. yes, 0.0009 of all jobs in the market are Scala positions following TIOBE.. :) but yes, IF Scala becomes the new standard, the ones that bet in the language today can be rich in few years.. otherwise they will only get a lot of fun in return of their beliefs :)

Felipe: Have a look at the slides, and you'll see that it is a course for "thinkers". I do the same with Scala as I would with Scheme, just in a more student-friendly syntax.