8.16
Racket Cheat Sheet
Primitives
Numbers
Literals | integer 1 rational 1/2 complex 1+2i floating 3.14 double 6.02e+23 hex #x29 octal #o32 binary #b010101 |
Arithmetic | +-*/quotientremaindermoduloadd1sub1maxminroundfloorceilingsqrtexptexplogsin...atan |
Compare | =<<=>>= |
Bitwise | bitwise-iorbitwise-andbitwise-xorbitwise-notarithmetic-shiftinteger-length |
Format | number->stringstring->numberreal->decimal-string |
Test | number?complex?...exact-nonnegative-integer?...zero?positive?negative?even?odd?exact?inexact? |
Misc | random |
Match Pattern | (?number?n)42 |
Strings
Literals | "Racket" quoting "a \" approaches!" unicode "λx:(μα.α→α).xx" |
Create | make-stringstringstring-appendbuild-stringstring-join |
Observe | string-lengthstring-refsubstringstring-splitin-string |
Modify | string-downcasestring-upcasestring-trim |
Test | string?string=?string<=?string-ci<=? |
Regexp | #rx"a|b"#rx"^c(a|d)+r$"regexp-quoteregexp-matchregexp-splitregexp-replaceregexp-replace* |
Match Pattern | (?string?s)"Banana?" |
Bytes
Literals | #"rawbytes\0" |
Create | make-bytesbytes |
Numbers | integer->integer-bytesreal->floating-point-bytes |
Observe | bytes-lengthbytes-refsubbytesin-bytes |
Modify | bytes-set!bytes-copy!bytes-fill! |
Conversion | bytes->string/utf-8 string->bytes/utf-8 |
Test | bytes?bytes=? |
Match Pattern | (?bytes?b)#"0xDEADBEEF" |
Other
Booleans | #t#fnotequal? |
Characters | #\a#\tab#\λchar?char->integerinteger->charchar<=?...char-alphabetic?... |
Symbols | 'Racketsymbol?eq?string->symbolgensym |
Boxes | box?boxunboxset-box!box-cas! |
Procedures | procedure?applycomposecompose1keyword-applyprocedure-renameprocedure-aritycurryarity-includes? |
Void | void?void |
Undefined | undefined |
Data
Lists
Create | emptylistlist*build-listfor/list |
Observe | empty?list?pair?lengthlist-refmembercountargminargmax |
Use | appendreversemapandmapormapfoldrin-list |
Modify | filterremove...sorttakedropsplit-atpartitionremove-duplicatesshuffle |
Match Pattern | (listabc)(list*abmore)(listtopmore...) |
Immutable Hash
Create | hashhasheq |
Observe | hash?hash-refhash-has-key?hash-countin-hashin-hash-keysin-hash-values |
Modify | hash-sethash-updatehash-remove |
Vector
Create | build-vectorvectormake-vectorlist->vector |
Observe | vector?vector-lengthvector-refin-vector |
Modify | vector-set!vector-fill!vector-copy!vector-map! |
Match Pattern | (vectorxyz)(vectorxycalabi–yau...) |
Streams
Create | streamstream*empty-stream |
Observe | stream-empty?stream-firststream-restin-stream |
Systems
Input/Output
Files
Miscellaneous
Time | current-secondscurrent-inexact-millisecondsdate->stringdate-display-format |
Command-Line Parsing | command-line |
FFI | ffi-lib_uint32..._funmallocfree |
Networking
Security
Custodians | make-custodiancustodian-shutdown-allcurrent-custodian |
Sandboxes | make-evaluatormake-module-evaluator |
Concurrency
Parallelism
Futures | futuretouchprocessor-countmake-fsemaphore... |
Places | dynamic-placeplaceplace-waitplace-waitplace-channel... |
Processes | subprocesssystem* |
Syntax (Beginner)
Basics
Modules | (module+mainbody...) (module+testbody...) (requiremod-path)(provideid) |
S-expressions | quote'(abc)quasiquoteunquote`(12,(+12)) |
Procedure Applications | (fnarg1arg2) keyword args(fnarg1#:keyarg2) (applyfnarg1(listarg2)) |
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!"]) andor |
Definitions | (definex1) (define(fy)(+xy)) |
Iteration | forfor/listfor* |
Blocks | beginwhenunless |
Require Sub-forms | prefix-inonly-inexcept-inrename-infor-syntaxfor-label... |
Provide Sub-forms | all-defined-outall-from-outrename-out...contract-out |
Structures
Definition | (structdillo(weightcolor)) |
Create | (definedanny(dillo17.5'purple)) |
Observe | (dillo?danny)(dillo-weightdanny)(dillo-colordanny) |
Modify | (struct-copydillodanny([weight18.0])) |
Match Pattern | (dillowc) |
Pattern Matching
Basics | (matchvalue[patbody]...) |
Definitions | (match-definepatvalue) |
Patterns | (quotedatum)(listlvp...)(list-no-orderpat...)(vectorlvp...)(struct-idpat...)(regexprx-exprpat)(orpat...)(andpat...)(?exprpat...) |
Syntax (Intermediate)
Basics
Mutation | set! |
Exceptions | errorwith-handlersraiseexit |
Promises | promise?delayforce |
Continuations | let/cclet/ecdynamic-windcall-with-continuation-promptabort-current-continuationcall-with-composable-continuation |
Parameters | make-parameterparameterize |
External Files Needed at Runtime | define-runtime-path |
Continuation Marks | continuation-markswith-continuation-markcontinuation-mark-set->list |
Multiple Values | valueslet-valuesdefine-valuescall-with-values |
Contracts
Basics | any/cor/cand/cfalse/cinteger-invector/clistoflist/c... |
Functions | ->->*->i |
Application | contract-outrecontract-outwith-contractdefine/contract |
Iteration
Sequences | in-rangein-naturalsin-listin-vectorin-portin-linesin-hashin-hash-keysin-hash-valuesin-directoryin-cyclestop-beforestop-afterin-stream |
Generators | generatoryieldin-generator |
Structures
Sub-structures | (struct2d(xy))(struct3d2d(z))(2d-x(3d123)) |
Mutation | (structmonster(type[hp#:mutable]))(definehealie(monster'slime10))(set-monster-hp!healie0) |
Transparency | (structcash($¢)#:transparent)(struct->vector(cash595)) |
Printing | (structnickname[nv]#:methodsgen:custom-write[(define(write-procnnpmode)(fprintfp(nickname-nnn)))])(displayln(nickname"evens"(in-range01002))) |
Serialization | (structtxn(whowhatwhere)#:prefab)(write(txn"Mustard""Spatula""Observatory")) |
Generics
Definition | define-generics |
Instantiation | (structeven-set()#:methodsgen:set[(define(set-member?sti)(even?i))]) |
Classes
Definition | interfaceclass* |
Instantiation | make-objectnewinstantiate |
Methods | sendsend/applysend/keyword-applysend*send+ |
Fields | get-fieldset-field! |
Mixins | mixin |
Traits | traittrait-sumtrait-excludetrait-rename... |
Contracts | class/cinstanceof/cis-a?/cimplementation?/csubclass?/c |
Syntactic Abstractions
Definition | define-syntaxdefine-simple-macrobegin-for-syntaxfor-syntax |
Templates | syntaxsyntax/locwith-syntax |
Parsing ()-Syntax | syntax-parsedefine-syntax-classpattern |
Syntax Objects | syntax-sourcesyntax-line...syntax->datumdatum->syntaxgenerate-temporariesformat-id |
Transformers | make-set!-transformermake-rename-transformerlocal-expandsyntax-local-valuesyntax-local-namesyntax-local-lift-expression... |
Syntax Parameters | define-syntax-parametersyntax-parameterizesyntax-parameter-value |
Parsing Raw Syntax | lexerparsercfg-parser |
Tools
Packages
Inspection | raco pkg show |
Finding | pkgs.racket-lang.org |
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 |
Extending DrRacket | drracket:language:simple-module-based-language->module-based-language-mixin |
Slides | slidestandard-fishcode |