This programming is a Racket

in Programming & Dev4 years ago

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!

Sort:  

Oh wow, that would have been really nice back when I was actually doing programming. Like I said back in your other post, I can't tell you the number of times I missed a semicolon or a brace and it caused all kinds of havoc in my code. One of the main reasons I didn't keep it up.

I remember back in the day that I would not find out about errors like that until I compiled my code, which could take a while. The tools have got better and of course the computers are quicker too. In one case I had a C++ program that took over an hour to compile and I persuaded the boss to get me a faster computer.

I find programming a very rewarding activity and I seem to have some talent for it.

!ENGAGE 10

The more you practise, the more talented you become. What do you think? Is the quote true?

Definitely, but you have to learn different things too.

!ENGAGE 10

Thank you for your engagement on this post, you have recieved ENGAGE tokens.

Thank you for your engagement on this post, you have recieved ENGAGE tokens.

"A programming post without a pun"
Steevc: You shall not pass!

Racket is based on Scheme, which came from Lisp.

The pile of languages that I don't know ever increases 😂

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

Very interesting, this is the second post today where I've seen an untraditional use of brackets (i.e. not function(args))

Also the way you do addition in Racket is quite odd as well, as my sums have always had the operator in the middle.

It's great to know about these things, so thanks for introducing me to them.

Thanks for posting in the programming community!

A lot of languages are based on C or similar and those tend to have the 'traditional' function calls. Lisp is really old and used different ideas. Even the operators are functions and so take take the same form as other functions. That consistency can actually make it easier to understand. Operator precedence is irrelevant as it is implicit from the way the functions are nested.

Forth is another interesting one that I used a little. It uses 'reverse Polish' notation so

25 * 50 + 10 => 25 50 * 10 +

That is to do with it using the stack for everything. It's not too bad once you get used to it, but switching between various languages could get confusing.

A lot of languages are based on C or similar and those tend to have the 'traditional' function calls.

That's interesting, I've never looked into why that is the case and of course I'm still quite young and so I've pretty much stuck to the high level languages. Such luxury! 😂

Reverse Polish sure is an interesting notation and looks like the sort of thing that can really mess with your head.

but switching between various languages could get confusing.

Yup, I had enough trouble switching from JS to Python the other day, let alone this 😂

Congratulations @steevc! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You got more than 45000 replies. Your next target is to reach 45500 replies.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

Update for regular authors