Racket Cheat Sheet

Korey Hinton –> My Blog –> Lisp –> Racket Cheat Sheet

April 16, 2014

DrRacket

If you are using the DrRacket IDE, it is recommended to put all your function definitions in the top half of the window where they can easily be added and saved. Then the interactive shell makes it easy to test them. Just hit run after making any function changes.

language

Before you can do anything you must define which language you are using.

#lang racket

booleans / predicates

#t or #f

Many functions that return boolean values have a question mark in the name. These are called predicates. Here is an example where a function that checks if a number is odd:

> (odd? 5)
#t
Type predicates

As you can see in the return value above odd? returns a truth value of either #t or #f.

; test if something is boolean:
> (boolean? #t)
#t

> (boolean? 5)
#f
Equality Predicates
; test if 2 things of the same type are equal
> (boolean=? #t #f)
#f

> (boolean=? #t #t)
#t

symbols

'mystring

A symbol is really just an immutable string.

> (symbol? 'mystring)
#t

> (symbol? 10)
#f

> (equal? 'a 'a)
#t

> (equal? 'a 'b)
#f

numbers

10 10.1 -10.0 +inf.0 -inf.f +nan.0 +nan.f
> 10
10

> 10.1
10.1
Boolean number functions
> (integer? 10)
#t

> (rational? 10.01)
#t

> (real? 10.01)
#t

; equals

> (eq? 10 10.0)
#f

> (eqv? 10 10.0)
#f

> (equal 10 10.0)
#f

> (= 10 10.0)
#t

primitive

+ - * / = void
> (require racket/base)
> (primitive? +)
#t

(primitive? void)
#t

string

"mystring"
'("some" "strings" "in" "a" "list")

match

(match exp
    [(pattern to match ...)         (code to execute if it matches ...)]
    [(another pattern to match ...) (code to execute if it matches ...)]
)

list

empty list
; 3 ways to get the empty list
'()
(list)
empty
construct a list
; 3 ways to construct a list
'(a b c)
(list 'a 'b 'c)
(cons 'a '(b c))

struct

; definition
> (struct employee (name age))

; instantiation
> (define Bob (employee 'Bob 42))

; accessors
> (employee-age Bob)
42
> (employee-name Bob)
'Bob

; type predicate is automatically created
> (employee? Bob)
#t

if

Important: everything that isn't #f is true!

(if conditional-expression
    then-expression
    else-expression)

> (if (boolean? #t)
      "you bet"
      "you fail")
"you bet"

cond

Also uses string-append and single quote escaping

(cond
    [(a-conditional-expression) (a-returned-result)]
    [(another-conditional-expression) (another-returned-result)]
    [else (a-default-returned-result)]
)

> (define name "Alice")
> (cond 
      [ (eq? name "Alice") (string-append name "\'s Adventures in Wonderland") ]
      [(eq? name "Gulliver") (string-append name "\'s Travels")]
      [else "Kidnapped"]   
   )
"Alice's Adventures in Wonderland"

and or

These 2 conditionals can greatly simplify code in certain instances without having to nest a bunch of ifs or using a cond to check multiple things at once. Here is an example where you might be trying to determine whether to close an order.

((and (packaged? package-packed-state) (shipped? package-shipment-state) (delivered? package-deliver-state))
    (or (canceled? package-canceled-state)))

when unless

These two functions are very useful and commonly used by experienced Racketeers.

(when ((and (packaged? package-packed-state) (shipped? package-shipment-state) (delivered? package-deliver-state))
    (or (canceled? package-canceled-state))) (close package))
(unless (or (canceled? package-canceled-state) (delivered? package-deliver-state)) (track package))

member

> (define alist '(a b c))
> (member 'a alist)
'(a b c)
> (member 'b alist)
'(b c)
> (member 'c alist)
'(c)

functions

(define (function-name param1 param2)
    (a-function-call param1) ; example function call, this happens first
    (another-function-name param2) ; this one gets called second
    (+ param1 param2) ; the result of the last expression is returned
)

recursion

> (define (length-finder alist)
      (if (empty? alist)
          0
          (add1 (length-finder (rest alist)))))
> (length-finder '(a b c))
3

comments

; a line comment
#| a 
   multi-line
   comment |#
#; (an-expression 
   (comment (goes until the very next open parenthesis is closed)))

Other Useful Tips

  • Hover to see definitions and associations or hover and press F1 to see documentation about a particular function

Date: 2014-04-17T05:22+0000

Author: Korey

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0