Racket Cheat Sheet

Racket Cheat Sheet

来源  http://docs.racket-lang.org/racket-cheat/index.html?q=Racket%20Cheat%20Sheet

 

Essentials

Sitesmain download docs git
Communitypackages users@ dev@ irc slack twitter
RunningPut #lang racket "Hello, world!" in hello.rkt and run racket hello.rkt
Primitives

Numbers

Literalsinteger 1 rational  1/2  complex 1+2ifloating 3.14 double 6.02e+23 hex #x29 octal #o32 binary #b010101
Arithmetic+ - * / quotient remaindermodulo add1 sub1 max min roundfloor ceiling sqrt expt exp logsin ... atan
Compare= < <= > >=
Bitwisebitwise-ior bitwise-andbitwise-xor bitwise-notarithmetic-shift integer-length
Formatnumber->string string->numberreal->decimal-string
Testnumber? complex? ... exact-nonnegative-integer? ... zero?positive? negative? even? odd?exact? inexact?
Miscrandom
Match Pattern(? number? n) 42

Strings

Literals"Racket" quoting "a \" approaches!"unicode "λx:(μα.α→α).xx"
Createmake-string string string-appendbuild-string string-join
Observestring-length string-refsubstring string-split in-string
Modifystring-downcase string-upcasestring-trim
Teststring? string=? string<=?string-ci<=?
Regexp#rx"a|b" #rx"^c(a|d)+r$" regexp-quote regexp-match regexp-splitregexp-replace regexp-replace*
Match Pattern(? string? s) "Banana?"
Systems

 

Syntax (Beginner)

Basics

Modules(module+ main body ...) 
(module+ test body ...) 
(require mod-path) (provideid)
S-expressionsquote '(a b c) quasiquoteunquote `(1 2 ,(+ 1 2))
Procedure Applications(fn arg1 arg2
keyword args (fn arg1 #:keyarg2
(apply fn arg1 (list arg2))
Procedures(lambda (x) x) (λ (x) x) 
(λ (x [opt 1]) (+ x opt)) 
(λ (x #:req key) (+ x key)) 
(λ (x #:opt [key 1]) (+ xkey))
Binding(let ([x 1] [y 2]) (+ x y)) 
(let* ([x 1] [x (+ x 1)]) x)
Conditionals(if (zero? x) 0 (/ 1 x)) 
(cond [(even? x) 0] [(odd? x)1] 
      [else "impossible!"]) 
and or
Definitions(define x 1) 
(define (f y) (+ x y))
Iterationfor for/list for*
Blocksbegin when unless
Require Sub-formsprefix-in only-in except-inrename-in for-syntax for-label ...
Provide Sub-formsall-defined-out all-from-outrename-out ... contract-out

Structures

Definition(struct dillo (weight color))
Create(define danny (dillo 17.5'purple))
Observe(dillo? danny) (dillo-weightdanny) (dillo-color danny)
Modify(struct-copy dillo danny([weight 18.0]))
Match Pattern(dillo w c)

Pattern Matching

Basics(match value [pat body] ...)
Definitions(match-define pat value)
Patterns(quote datum) (list lvp ...)(list-no-order pat ...) (vectorlvp ...) (struct-id pat ...)(regexp rx-expr pat) (or pat...) (and pat ...) (? expr pat...)
Syntax (Intermediate)

Structures

Sub-structures(struct 2d (x y)) (struct 3d2d (z)) (2d-x (3d 1 2 3))
Mutation(struct monster (type [hp#:mutable])) (define healie(monster 'slime 10)) (set-monster-hp! healie 0)
Transparency(struct cash ($ ¢)#:transparent) (struct->vector (cash 5 95))
Printing(struct nickname [n v]#:methods gen:custom-write[(define (write-proc nn pmode) (fprintf p (nickname-nnn)))]) (displayln (nickname"evens" (in-range 0 100 2)))
Serialization(struct txn (who what where)#:prefab) (write (txn"Mustard" "Spatula""Observatory"))

Generics

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

Packages

Inspectionraco pkg show
Findingpkgs.racket-lang.org
Installingraco pkg install
Updatingraco pkg update
Removingraco pkg remove

Miscellaneous

Compilingraco make program.rkt
Testingraco test program.rkt a-directory
Building Executablesraco exe program.rkt
Extending DrRacketdrracket:language:simple-module-based-language->module-based-language-mixin
Slidesslide standard-fish code

 

================= End

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值