Racket is a descendant of Scheme which is a descendant of Lisp. Interestingly enough, Racket was the inspiration for Clojure.
What makes a Lisp language so special?
We’ll take a closer look at each one of these topics shortly:
Operators → Operators (+ – * /) come first, then the operands (1 5 8).
Parentheses → Everything is surrounded by parenthesis.
Expressions → Everything is an expression. Which means that everything can be combined with almost everything else.
What do we need to code in Racket?
In this post, we’re going to get a small introduction to Racket, a programming language part of the Lisp family of languages.
Racket (1995) → If we’re on a Mac, the best way is to use homebrew and type on the terminal window:
$ brew install --cask racket
For other systems, you can refer to the download page.
Operators
Let’s open up the DrRacket which is Racket’s own Integrated Development Environment (IDE). On the terminal type:
$ drracket
The first time we run it, it will prompt us to choose a language. Choose the first one:
Then save the file using Crtl + S and call it Sandbox.rkt or whatever you prefer. Then type:
#lang racket
(+ 1 2 3)
Here we’re simply saying, we’re going to sum this list of numbers. Everything on Racket is either a single value or a list. Press the Run button or Command + R.
Let’s say now that we would like to multiply the result by 2.
#lang racket
(* (+ 1 2 3) 2)
Starting to look weird, huh? Simply we’re saying that we want to multiply the result of the sum operation by 2. So we can infer that nested parentheses get evaluated first. So we first complete the addition `1 + 2 + 3`, take the result and multiply it by 2.
Parentheses
Now we can see why everything is surrounded by parentheses in Lisp, but there’s more. This is how we would declare and call a function:
Here, our function name is HelloFunction and it has a parameter called name. As we can see the function name and parameter are surrounded by parentheses. The body of the function, which is surrounded with parenthesis as well, states that we’re going to append the string Hello and the parameter name. The whole function is surrounded by parentheses. When calling the function, we surround it with parentheses and pass the parameter.
Expressions
We said that everything is an expression, so what do we mean by that exactly? Well, we can do something like this:
Here we are defining a lambda which is an anonymous function (Anonymous functions are useful because they allow us to create functions inline without the need of actually declaring them. They are usually faster and consume less resources), but assigning that into a variable, so we can call the variable in order to run the lambda.
We’re creating a lambda and assigning it to a variable called myLambda. This lambda will sum 2 and 3 together. Then, we’re multiplying the result of the lambda by 8.
What does a complete Racket application look like?
Let’s create a simple example of a Fibonacci sequence. Where each number equals the sum of its predecessors.
So if we input 5, the answer will be 0 1 1 2 3 5.
#lang racket
# Here we define a function called showFib that gets num as the argument
(define (showFib num)
# It will display on the screen the result of calling the fib function passing num, 0 and 1
(display (fib num 0 1)))
# Here we define a method called fib that gets three parameters: num, a and b.
(define (fib num a b)
# If a > 0 and num > 1
( cond [(and (> a 0) (> num 1))
# Sum a and b and add the result to the output
(append (cons (+ a b) '())
# call the fib function again, subtracting one from num and adding a plus b
(fib (sub1 num) (+ a b) a))]
# If a equals 0
[(= a 0)
# Add a and b to the output
(append (append (cons a (cons b '()))
# Sum a and b and add it to the output
(cons (+ a b) '()))
# Call the fib function again, subtracting one from num and adding a plus b
(fib(sub1 num)(+ a b) b))]
# If num equals 1, then we can stop and append a terminator to the list
[(= 1 num) (append '())]))
What about a big number? How about 5000?
Hard to see but the last number is 1045 digits long.
Learning Racket or Lisp is great to bend your mind on how programming languages work.