This programming is a Racket

I posted previously about the Coursera courses I am doing on programming languages. I am now onto the second in the series. As with part A this uses a language I was not familiar. Last time it was ML (metalanguage) and this time it is Racket.

image.png
Racket is based on Scheme, which came from Lisp. Having read about Lisp I was interested to see how this worked. The course suggests using the DrRacket programming environment that provides some nice debugging features.

I racket everything is coded as a function and parameters in brackets, e.g.

(+ 2 3)

This uses the + function to add a couple of numbers together. Unlike in many other languages the brackets have a precise meaning of executing a function and adding in extra layers changes that meaning.

; This calls a function
(somefunc 2 3)
; This calls a function that is returned by a function
((somefunc 2 3))

As in the ML course a lot of use is made of recursion as an alternative to loops. It is simple to create functions that emulate various loop structures. These are not courses to make you an expert in these languages. They are used to teach various principles of programming. Both are functional languages, but where ML uses static types with strict checking Racket has dynamic types that can result in runtime errors if you get them wrong.

Functions are simple to create. I like that the language does not involve lots of extra keywords and other 'cruft'. You can end up with a lot of closing brackets (or braces or parenteses according to local usage) at the end of a section of code, but the IDE tells you if you have the right number. The XKCD comic has played with this idea.

> (define (square x) (* x x))
> (square 23)
529

The course is very intensive with several hours of video lectures plus associated notes that you really need to digest. The homework assignment was to implement an interpreter for a simple language MUPL (Made Up Programming Language). It has a set of functions that can be used in the same way as Racket code, but requires the interpreter function to actually execute them. This makes use of the struct functionality to define datatypes.

(struct add  (e1 e2)  #:transparent)
; Is this expression an add?
(add? e)
; Get the first parameter from an expression
(add-e1 e)

These datatypes give you a set of functions to test if something is of that type and to extract the parameters. The interpreter basically checks for each one and has appropriate Racket code to do what is needed. In less than 100 lines of code you get something that can handle recursive functions. Both ML and Racket can be used to implement new languages to serve specific purposes, which can occur in various research including artificial intelligence.

I will admit I struggled with the homework. Some parts were trivial, but the part involved with calling functions took me a few hours to figure out. My solution may have been slightly over-complicated, but it passed the test suite with 100%. There is a peer review element too where other students may pick fault with it. The work I reviewed was all very consistent with the suggested solutions. That will be partly due to everyone following the same course. The language has a lot of extra libraries that could make these simpler. It can do graphics and networking which we did not touch on at all.

I have one more week of this course that goes into the merits of static and dynamic types with a final quiz to complete. The third and final course uses Ruby to get into object-oriented programming. I aim to complete that too. Having a broad experience of programming makes it easier to learn new languages.

Code hard!

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center