Skip to content

Racket Basics — S-Expressions, Functions, and the DrRacket IDE

DodaTech Updated 2026-06-29 3 min read

In this tutorial, you will learn about Racket Basics. We cover key concepts, practical examples, and best practices to help you master this topic.

Racket is a dialect of Scheme/Lisp designed for learning, language prototyping, and research. Its minimalist syntax — based on s-expressions — makes the language's structure transparent.

In this tutorial, you'll learn Racket fundamentals. Racket is used in university courses and programming language research, and its principles influence security tool design at DodaTech where DSLs are used for scan rule configuration.

What You'll Learn

  • S-expression syntax
  • Numbers, booleans, strings
  • Defining functions
  • Conditionals (if, cond)
  • Lists and list operations
  • DrRacket IDE basics

S-Expressions

; Racket uses prefix notation: (function arg1 arg2)
(+ 1 2)          ; => 3
(* 3 4 5)        ; => 60
(/ 10 3)         ; => 3 1/3 (exact rational)
(sqrt 16)        ; => 4

; Nested expressions
(* (+ 2 3) (- 10 4))  ; => 30

Data Types

; Numbers
42          ; integer
3.14        ; float
3/4         ; exact rational
1+2i        ; complex

; Booleans
#t          ; true
#f          ; false

; Strings
"Hello, Racket!"
(string-append "Hello" " " "World")  ; => "Hello World"
(string-length "racket")             ; => 6

; Symbols
'hello
'this-is-a-symbol

; Characters
#\A
#\space

Defining Functions

; Define a function
(define (square x)
  (* x x))

(square 5)  ; => 25

; Multiple parameters
(define (add a b)
  (+ a b))

(add 3 4)  ; => 7

; Lambda (anonymous function)
(map (lambda (x) (* x 2)) '(1 2 3))  ; => '(2 4 6)

; Shorthand for lambda
(map add1 '(1 2 3))  ; => '(2 3 4)

Conditionals

; if: (if condition true-expr false-expr)
(define (can-vote? age)
  (if (>= age 18)
      "Yes"
      "No"))

(can-vote? 20)  ; => "Yes"
(can-vote? 15)  ; => "No"

; cond (multi-branch)
(define (grade score)
  (cond
    [(>= score 90) "A"]
    [(>= score 80) "B"]
    [(>= score 70) "C"]
    [(>= score 60) "D"]
    [else "F"]))

(grade 85)  ; => "B"

Lists

; Lists are the fundamental data structure
'(1 2 3 4)           ; Quote prevents evaluation
(list 1 2 3 4)       ; Same result
(cons 1 '(2 3 4))    ; => '(1 2 3 4)

; Accessing lists
(first '(1 2 3))     ; => 1
(rest '(1 2 3))      ; => '(2 3)
(second '(1 2 3))    ; => 2
(length '(1 2 3))     ; => 3

; Building lists
(append '(1 2) '(3 4))    ; => '(1 2 3 4)
(reverse '(1 2 3))        ; => '(3 2 1)
(take '(1 2 3 4) 2)       ; => '(1 2)

; List operations
(map sqr '(1 2 3 4))     ; => '(1 4 9 16)
(filter even? '(1 2 3 4)) ; => '(2 4)
(foldl + 0 '(1 2 3 4))    ; => 10
(member 3 '(1 2 3 4))     ; => '(3 4)

Recursion

; Racket uses recursion instead of loops
(define (factorial n)
  (if (<= n 1)
      1
      (* n (factorial (- n 1)))))

(factorial 5)  ; => 120

; Tail recursion (efficient)
(define (sum lst)
  (define (helper acc remaining)
    (if (empty? remaining)
        acc
        (helper (+ acc (first remaining)) (rest remaining))))
  (helper 0 lst))

(sum '(1 2 3 4))  ; => 10

DrRacket

# Install Racket:
# apt install racket
# or download from https://download.racket-lang.org/

Practice Questions

  1. Write a function that computes the nth Fibonacci number recursively.

  2. Use map to square every number in a list.

  3. Write a cond expression that categorizes a temperature as freezing/cold/warm/hot.

  4. Write a recursive function to reverse a list (without using built-in reverse).

  5. Use filter to extract all even numbers from a list.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro