9.0
top
← prev up next →

Racket Cheat SheetπŸ”— i

Jay McCarthy

Essentials
Running Put #lang racket "Hello, world!" in hello.rkt and run racket hello.rkt
Primitives

Numbers

Literals integer 1 rational 1/2 complex 1+2i floating 3.14 double 6.02e+23 hex #x29 octal #o32 binary #b010101
Compare = < <= > >=
Misc random
Match Pattern (?number? n)42

Strings

Literals "Racket" quoting "a \" approaches!" unicode "λx:(μα.α→α).xx"
Match Pattern (?string? s)"Banana?"
Data
Systems
Syntax (Beginner)

Basics

Modules (module+ mainbody...)
(module+ testbody...)
(require mod-path)(provide id)
S-expressions quote '(abc)quasiquote unquote `(12,(+ 12))
Procedure Applications (fnarg1arg2)
keyword args (fnarg1#:keyarg2)
(apply fnarg1(list arg2))
Procedures (lambda (x)x)(λ (x)x)
(λ (x[opt1])(+ xopt))
(λ (x#:reqkey)(+ xkey))
(λ (x#:opt[key1])(+ xkey))
Binding (let ([x1][y2])(+ xy))
(let* ([x1][x(+ x1)])x)
Conditionals (if (zero? x)0(/ 1x))
(cond [(even? x)0][(odd? x)1]
[else "impossible!"])
and or
Definitions (define x1)
(define (fy)(+ xy))
Iteration for for/list for*
Blocks begin when unless

Structures

Definition (struct dillo(weightcolor))
Create (define danny(dillo17.5'purple))
Observe (dillo?danny)(dillo-weightdanny)(dillo-colordanny)
Modify (struct-copy dillodanny([weight18.0]))
Match Pattern (dillowc)

Pattern Matching

Basics (match value[patbody]...)
Definitions (match-define patvalue)
Patterns (quotedatum)(list lvp...)(list-no-orderpat...)(vectorlvp...)(struct-idpat...)(regexprx-exprpat)(orpat...)(andpat...)(?exprpat...)
Syntax (Intermediate)

Structures

Sub-structures (struct 2d(xy))(struct 3d2d(z))(2d-x(3d123))
Mutation (struct monster(type[hp#:mutable]))(define healie(monster'slime10))(set-monster-hp!healie0)
Transparency (struct cash($¢)#:transparent)(struct->vector (cash595))
Printing (struct nickname[nv]#:methodsgen:custom-write [(define (write-procnnpmode)(fprintf p(nickname-nnn)))])(displayln (nickname"evens"(in-range 01002)))
Serialization (struct txn(whowhatwhere)#:prefab)(write (txn"Mustard""Spatula""Observatory"))

Generics

Definition define-generics
Instantiation (struct even-set()#:methodsgen:set [(define (set-member? sti)(even? i))])
Tools

Packages

Inspection raco pkg show
Installing raco pkg install
Updating raco pkg update
Removing raco pkg remove

Miscellaneous

Compiling raco make program.rkt
Testing raco test program.rkt a-directory
Building Executables raco exe program.rkt

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /