RacketCheat-SheetReferenceSyntax

The Ultimate Racket Programming Cheat Sheet

2.155 min read
Md Nasim SheikhMd Nasim Sheikh
Share:

Bookmark this page. It covers 90% of what you use daily.

Advertisement

Basic Syntax

ConceptSyntaxExample
Define Var(define id val)(define x 10)
Define Func(define (id args) body)(define (add x y) (+ x y))
Lambda(lambda (args) body)(lambda (x) (* x x))
Call(func arg ...)(add 5 10)

Control Flow

; If
(if (> x 0) "Pos" "Neg")

; Cond (Switch)
(cond 
  [(> x 0) "Pos"]
  [(= x 0) "Zero"]
  [else "Neg"])

; Match (Pattern Matching)
(match lst
  ['() "Empty"]
  [(list a b) (+ a b)])

Lists (The Bread & Butter)

(cons 1 '(2 3))      ; '(1 2 3)
(first '(1 2 3))     ; 1
(rest '(1 2 3))      ; '(2 3)
(map add1 '(1 2 3))  ; '(2 3 4)
(filter even? '(1 2 3)) ; '(2)
(foldl + 0 '(1 2 3)) ; 6

Structs

(struct pt (x y) #:transparent)
(define p (pt 10 20))
(pt-x p) ; 10

Modules

(provide my-func)
(require "other-file.rkt")
(require racket/math)

Debugging

  • (displayln x): Print line.
  • (error "Msg"): Crash program.
  • (time (expensive-op)): Measure speed.

Advertisement

Need more details? Check out our Full Course.

Md Nasim Sheikh
Written by

Md Nasim Sheikh

Software Developer at softexForge

Verified Author150+ Projects
Published:

You May Also Like