Learn you a Lisp (Racket)

Ever wondered what Lisp is all about? Using Racket we’re going to learn the fundamentals of the Lisp programming family.

Learn you a Lisp (Racket)

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.


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:

Dr Racket IDE. Choose Racket lang

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.

Operators example

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.


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:

#lang racket

(define (HelloFunction name) 
        (string-append "Hello " name))

(HelloFunction "Blag")

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.

Racket parenthesis example


We said that everything is an expression, so what do we mean by that exactly? Well, we can do something like this:

#lang racket

(define (sum_or_by num)
((if (< 5 num) + *) 10 20))

(sum_or_by 6)
(sum_or_by 2)

If the number we’re passing as a parameter is lower than 5, then sum 10 and 20, but if the number is bigger than 5, then multiply 10 and 20.

Expressions example

We can also do something like this:

#lang racket

(define myLambda (lambda () (print "Hello Racket!")))


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.

Lambdas example

And what about this?

#lang racket

(define myLambda (lambda () (+ 2 3)))

(* (myLambda) 8)

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.

Another Lambdas example

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 '())]))
Fibonacci application

What about a big number? How about 5000?

Fibonacci of 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.

Lisp XKCD comic
Image taken from https://xkcd.com/

Watch the livestream Learn you a Lisp (Racket) | Coding with Nylas | Episode 35

To learn more about Nylas, follow our documentation.

Don’t miss the action, watch our LiveStream Coding with Nylas:

You May Also Like

A Small Guide to Virtual Calendars
hero banner
How to Mail Merge in Outlook Step by Step
13+ Best Calendar Apps
13+ Best Calendar Apps

Subscribe for our updates

Please enter your email address and receive the latest updates.