<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # [X分鐘速成Y](http://learnxinyminutes.com/) ## 其中 Y=racket 源代碼下載:?[learnracket-zh.rkt](http://learnxinyminutes.com/docs/files/learnracket-zh.rkt) Racket是Lisp/Scheme家族中的一個通用的,多范式的編程語言。 非常期待您的反饋!你可以通過[@th3rac25](http://twitter.com/th3rac25)或以用戶名為 th3rac25 的Google郵箱服務和我取得聯系 ~~~ #lang racket ; 聲明我們使用的語言 ;;; 注釋 ;; 單行注釋以分號開始 #| 塊注釋 可以橫跨很多行而且... #| 可以嵌套 |# |# ;; S表達式注釋忽略剩下的表達式 ;; 在調試的時候會非常有用 #; (被忽略的表達式) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 1\. 原始數據類型和操作符 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 數字 9999999999999999999999 ; 整數 #b111 ; 二進制數字 => 7 #o111 ; 八進制數字 => 73 #x111 ; 十六進制數字 => 273 3.14 ; 實數 6.02e+23 1/2 ; 有理數 1+2i ; 復數 ;; 函數調用寫作(f x y z ...) ;; 在這里 f 是一個函數, x, y, z, ... 是參數 ;; 如果你想創建一個列表數據的字面量, 使用 ' 來阻止它們 ;; 被求值 '(+ 1 2) ; => (+ 1 2) ;; 接下來,是一些數學運算 (+ 1 1) ; => 2 (- 8 1) ; => 7 (* 10 2) ; => 20 (expt 2 3) ; => 8 (quotient 5 2) ; => 2 (remainder 5 2) ; => 1 (/ 35 5) ; => 7 (/ 1 3) ; => 1/3 (exact->inexact 1/3) ; => 0.3333333333333333 (+ 1+2i 2-3i) ; => 3-1i ;;; 布爾類型 #t ; 為真 #f ; 為假,#f 之外的任何值都是真 (not #t) ; => #f (and 0 #f (error "doesn't get here")) ; => #f (or #f 0 (error "doesn't get here")) ; => 0 ;;; 字符 #\A ; => #\A #\λ ; => #\λ #\u03BB ; => #\λ ;;; 字符串是字符組成的定長數組 "Hello, world!" "Benjamin \"Bugsy\" Siegel" ; \是轉義字符 "Foo\tbar\41\x21\u0021\a\r\n" ; 包含C語言的轉義字符,和Unicode "λx:(μα.α→α).xx" ; 字符串可以包含Unicode字符 ;; 字符串可以相加 (string-append "Hello " "world!") ; => "Hello world!" ;; 一個字符串可以看做是一個包含字符的列表 (string-ref "Apple" 0) ; => #\A ;; format 可以用來格式化字符串 (format "~a can be ~a" "strings" "formatted") ;; 打印字符串非常簡單 (printf "I'm Racket. Nice to meet you!\n") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 2\. 變量 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 你可以使用 define 定義一個變量 ;; 變量的名字可以使用任何字符除了: ()[]{}",'`;#|\ (define some-var 5) some-var ; => 5 ;; 你也可以使用Unicode字符 (define ? subset?) (? (set 3 2) (set 1 2 3)) ; => #t ;; 訪問未賦值的變量會引發一個異常 ; x ; => x: undefined ... ;; 本地綁定: `me' 被綁定到 "Bob",并且只在 let 中生效 (let ([me "Bob"]) "Alice" me) ; => "Bob" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3\. 結構和集合 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 結構體 (struct dog (name breed age)) (define my-pet (dog "lassie" "collie" 5)) my-pet ; => #<dog> (dog? my-pet) ; => #t (dog-name my-pet) ; => "lassie" ;;; 對 (不可變的) ;; `cons' 返回對, `car' 和 `cdr' 從對中提取第1個 ;; 和第2個元素 (cons 1 2) ; => '(1 . 2) (car (cons 1 2)) ; => 1 (cdr (cons 1 2)) ; => 2 ;;; 列表 ;; 列表由鏈表構成, 由 `cons' 的結果 ;; 和一個 `null' (或者 '()) 構成,后者標記了這個列表的結束 (cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3) ;; `list' 給列表提供了一個非常方便的可變參數的生成器 (list 1 2 3) ; => '(1 2 3) ;; 一個單引號也可以用來表示一個列表字面量 '(1 2 3) ; => '(1 2 3) ;; 仍然可以使用 `cons' 在列表的開始處添加一項 (cons 4 '(1 2 3)) ; => '(4 1 2 3) ;; `append' 函數可以將兩個列表合并 (append '(1 2) '(3 4)) ; => '(1 2 3 4) ;; 列表是非常基礎的類型,所以有*很多*操作列表的方法 ;; 下面是一些例子: (map add1 '(1 2 3)) ; => '(2 3 4) (map + '(1 2 3) '(10 20 30)) ; => '(11 22 33) (filter even? '(1 2 3 4)) ; => '(2 4) (count even? '(1 2 3 4)) ; => 2 (take '(1 2 3 4) 2) ; => '(1 2) (drop '(1 2 3 4) 2) ; => '(3 4) ;;; 向量 ;; 向量是定長的數組 #(1 2 3) ; => '#(1 2 3) ;; 使用 `vector-append' 方法將2個向量合并 (vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6) ;;; Set(翻譯成集合也不太合適,所以不翻譯了..) ;; 從一個列表創建一個Set (list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3) ;; 使用 `set-add' 增加一個成員 ;; (函數式特性: 這里會返回一個擴展后的Set,而不是修改輸入的值) (set-add (set 1 2 3) 4) ; => (set 1 2 3 4) ;; 使用 `set-remove' 移除一個成員 (set-remove (set 1 2 3) 1) ; => (set 2 3) ;; 使用 `set-member?' 測試成員是否存在 (set-member? (set 1 2 3) 1) ; => #t (set-member? (set 1 2 3) 4) ; => #f ;;; 散列表 ;; 創建一個不變的散列表 (可變散列表的例子在下面) (define m (hash 'a 1 'b 2 'c 3)) ;; 根據鍵取得值 (hash-ref m 'a) ; => 1 ;; 獲取一個不存在的鍵是一個異常 ; (hash-ref m 'd) => 沒有找到元素 ;; 你可以給不存在的鍵提供一個默認值 (hash-ref m 'd 0) ; => 0 ;; 使用 `hash-set' 來擴展一個不可變的散列表 ;; (返回的是擴展后的散列表而不是修改它) (define m2 (hash-set m 'd 4)) m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3)) ;; 記住,使用 `hash` 創建的散列表是不可變的 m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;; 使用 `hash-remove' 移除一個鍵值對 (函數式特性,m并不變) (hash-remove m 'a) ; => '#hash((b . 2) (c . 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3\. 函數 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 使用 `lambda' 創建函數 ;; 函數總是返回它最后一個表達式的值 (lambda () "Hello World") ; => #<procedure> ;; 也可以使用 Unicode 字符 `λ' (λ () "Hello World") ; => 同樣的函數 ;; 使用括號調用一個函數,也可以直接調用一個 lambda 表達式 ((lambda () "Hello World")) ; => "Hello World" ((λ () "Hello World")) ; => "Hello World" ;; 將函數賦值為一個變量 (define hello-world (lambda () "Hello World")) (hello-world) ; => "Hello World" ;; 你可以使用函數定義的語法糖來簡化代碼 (define (hello-world2) "Hello World") ;; `()`是函數的參數列表 (define hello (lambda (name) (string-append "Hello " name))) (hello "Steve") ; => "Hello Steve" ;; 同樣的,可以使用語法糖來定義: (define (hello2 name) (string-append "Hello " name)) ;; 你也可以使用可變參數, `case-lambda' (define hello3 (case-lambda [() "Hello World"] [(name) (string-append "Hello " name)])) (hello3 "Jake") ; => "Hello Jake" (hello3) ; => "Hello World" ;; ... 或者給參數指定一個可選的默認值 (define (hello4 [name "World"]) (string-append "Hello " name)) ;; 函數可以將多余的參數放到一個列表里 (define (count-args . args) (format "You passed ~a args: ~a" (length args) args)) (count-args 1 2 3) ; => "You passed 3 args: (1 2 3)" ;; ... 也可以使用不帶語法糖的 `lambda' 形式: (define count-args2 (lambda args (format "You passed ~a args: ~a" (length args) args))) ;; 你可以混用兩種用法 (define (hello-count name . args) (format "Hello ~a, you passed ~a extra args" name (length args))) (hello-count "Finn" 1 2 3) ; => "Hello Finn, you passed 3 extra args" ;; ... 不帶語法糖的形式: (define hello-count2 (lambda (name . args) (format "Hello ~a, you passed ~a extra args" name (length args)))) ;; 使用關鍵字 (define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args) (format "~a ~a, ~a extra args" g name (length args))) (hello-k) ; => "Hello World, 0 extra args" (hello-k 1 2 3) ; => "Hello World, 3 extra args" (hello-k #:greeting "Hi") ; => "Hi World, 0 extra args" (hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args" (hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6) ; => "Hi Finn, 6 extra args" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 4\. 判斷是否相等 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 判斷數字使用 `=' (= 3 3.0) ; => #t (= 2 1) ; => #f ;; 判斷對象使用 `eq?' (eq? 3 3) ; => #t (eq? 3 3.0) ; => #f (eq? (list 3) (list 3)) ; => #f ;; 判斷集合使用 `equal?' (equal? (list 'a 'b) (list 'a 'b)) ; => #t (equal? (list 'a 'b) (list 'b 'a)) ; => #f ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 5\. 控制結構 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 條件判斷 (if #t ; 測試表達式 "this is true" ; 為真的表達式 "this is false") ; 為假的表達式 ; => "this is true" ;; 注意, 除 `#f` 之外的所有值都認為是真 (member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo) (if (member 'Groucho '(Harpo Groucho Zeppo)) 'yep 'nope) ; => 'yep ;; `cond' 會進行一系列的判斷來選擇一個結果 (cond [(> 2 2) (error "wrong!")] [(< 2 2) (error "wrong again!")] [else 'ok]) ; => 'ok ;;; 模式匹配 (define (fizzbuzz? n) (match (list (remainder n 3) (remainder n 5)) [(list 0 0) 'fizzbuzz] [(list 0 _) 'fizz] [(list _ 0) 'buzz] [_ #f])) (fizzbuzz? 15) ; => 'fizzbuzz (fizzbuzz? 37) ; => #f ;;; 循環 ;; 循環可以使用遞歸(尾遞歸) (define (loop i) (when (< i 10) (printf "i=~a\n" i) (loop (add1 i)))) (loop 5) ; => i=5, i=6, ... ;; 類似的,可以使用 `let` 定義 (let loop ((i 0)) (when (< i 10) (printf "i=~a\n" i) (loop (add1 i)))) ; => i=0, i=1, ... ;; 看上面的例子怎么增加一個新的 `loop' 形式, 但是 Racket 已經有了一個非常 ;; 靈活的 `for' 了: (for ([i 10]) (printf "i=~a\n" i)) ; => i=0, i=1, ... (for ([i (in-range 5 10)]) (printf "i=~a\n" i)) ; => i=5, i=6, ... ;;; 其他形式的迭代 ;; `for' 允許在很多數據結構中迭代: ;; 列表, 向量, 字符串, Set, 散列表, 等... (for ([i (in-list '(l i s t))]) (displayln i)) (for ([i (in-vector #(v e c t o r))]) (displayln i)) (for ([i (in-string "string")]) (displayln i)) (for ([i (in-set (set 'x 'y 'z))]) (displayln i)) (for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))]) (printf "key:~a value:~a\n" k v)) ;;; 更多復雜的迭代 ;; 并行掃描多個序列 (遇到長度小的就停止) (for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j)) ; => 0:x 1:y 2:z ;; 嵌套循環 (for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j)) ; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z ;; 帶有條件判斷的 `for` (for ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10)) (printf "i=~a\n" i)) ; => i=6, i=8, i=10 ;;; 更多的例子幫助你加深理解.. ;; 和 `for' 循環非常像 -- 收集結果 (for/list ([i '(1 2 3)]) (add1 i)) ; => '(2 3 4) (for/list ([i '(1 2 3)] #:when (even? i)) i) ; => '(2) (for/list ([i 10] [j '(x y z)]) (list i j)) ; => '((0 x) (1 y) (2 z)) (for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10)) i) ; => '(6 8 10) (for/hash ([i '(1 2 3)]) (values i (number->string i))) ; => '#hash((1 . "1") (2 . "2") (3 . "3")) ;; 也有很多其他的內置方法來收集循環中的值: (for/sum ([i 10]) (* i i)) ; => 285 (for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000 (for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t (for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t ;; 如果需要合并計算結果, 使用 `for/fold' (for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10 ;; (這個函數可以在大部分情況下替代普通的命令式循環) ;;; 異常 ;; 要捕獲一個異常,使用 `with-handlers' 形式 (with-handlers ([exn:fail? (lambda (exn) 999)]) (+ 1 "2")) ; => 999 (with-handlers ([exn:break? (lambda (exn) "no time")]) (sleep 3) "phew") ; => "phew", 如果你打斷了它,那么結果 => "no time" ;; 使用 `raise' 拋出一個異常后者其他任何值 (with-handlers ([number? ; 捕獲拋出的數字類型的值 identity]) ; 將它們作為普通值 (+ 1 (raise 2))) ; => 2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 6\. 可變的值 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 使用 `set!' 給一個已經存在的變量賦一個新值 (define n 5) (set! n (add1 n)) n ; => 6 ;; 給那些明確地需要變化的值使用 `boxes` (在其他語言里類似指針 ;; 或者引用) (define n* (box 5)) (set-box! n* (add1 (unbox n*))) (unbox n*) ; => 6 ;; 很多 Racket 詩句類型是不可變的 (對,列表,等),有一些既是可變的 ;; 又是不可變的 (字符串,向量,散列表 ;; 等...) ;; 使用 `vector' 或者 `make-vector' 創建一個可變的向量 (define vec (vector 2 2 3 4)) (define wall (make-vector 100 'bottle-of-beer)) ;; 使用 `vector-set!` 更新一項 (vector-set! vec 0 1) (vector-set! wall 99 'down) vec ; => #(1 2 3 4) ;; 創建一個空的可變散列表,然后操作它 (define m3 (make-hash)) (hash-set! m3 'a 1) (hash-set! m3 'b 2) (hash-set! m3 'c 3) (hash-ref m3 'a) ; => 1 (hash-ref m3 'd 0) ; => 0 (hash-remove! m3 'a) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 7\. 模塊 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 模塊讓你將你的代碼組織為多個文件,成為可重用的模塊, ;; 在這里,我們使用嵌套在本文的整個大模塊 ;; 里的子模塊(從 "#lang" 這一行開始) (module cake racket/base ; 基于 racket/base 定義一個 `cake` 模塊 (provide print-cake) ; 這個模塊導出的函數 (define (print-cake n) (show " ~a " n #\.) (show " .-~a-. " n #\|) (show " | ~a | " n #\space) (show "---~a---" n #\-)) (define (show fmt n ch) ; 內部函數 (printf fmt (make-string n ch)) (newline))) ;; 使用 `require` 從模塊中得到所有 `provide` 的函數 (require 'cake) ; 這里的 `'`表示是本地的子模塊 (print-cake 3) ; (show "~a" 1 #\A) ; => 報錯, `show' 沒有被導出,不存在 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 8\. 類和對象 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 創建一個 fish% 類(%是給類綁定用的) (define fish% (class object% (init size) ; 初始化的參數 (super-new) ; 父類的初始化 ;; 域 (define current-size size) ;; 公共方法 (define/public (get-size) current-size) (define/public (grow amt) (set! current-size (+ amt current-size))) (define/public (eat other-fish) (grow (send other-fish get-size))))) ;; 創建一個 fish% 類的示例 (define charlie (new fish% [size 10])) ;; 使用 `send' 調用一個對象的方法 (send charlie get-size) ; => 10 (send charlie grow 6) (send charlie get-size) ; => 16 ;; `fish%' 是一個普通的值,我們可以用它來混入 (define (add-color c%) (class c% (init color) (super-new) (define my-color color) (define/public (get-color) my-color))) (define colored-fish% (add-color fish%)) (define charlie2 (new colored-fish% [size 10] [color 'red])) (send charlie2 get-color) ;; 或者,不帶名字 (send (new (add-color fish%) [size 10] [color 'red]) get-color) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 9\. 宏 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 宏讓你擴展這門語言的語法 ;; 讓我們定義一個while循環 (define-syntax-rule (while condition body ...) (let loop () (when condition body ... (loop)))) (let ([i 0]) (while (< i 10) (displayln i) (set! i (add1 i)))) ;; 宏是安全的,你不能修改現有的變量 (define-syntax-rule (swap! x y) ; !表示會修改 (let ([tmp x]) (set! x y) (set! y tmp))) (define tmp 2) (define other 3) (swap! tmp other) (printf "tmp = ~a; other = ~a\n" tmp other) ;; 變量 `tmp` 被重命名為 `tmp_1` ;; 避免名字沖突 ;; (let ([tmp_1 tmp]) ;; (set! tmp other) ;; (set! other tmp_1)) ;; 但它們仍然會導致錯誤代碼,比如: (define-syntax-rule (bad-while condition body ...) (when condition body ... (bad-while condition body ...))) ;; 這個宏會掛掉,它產生了一個無限循環,如果你試圖去使用它 ;; 編譯器會進入死循環 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 10\. 契約 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 契約限制變量從模塊中導入 (module bank-account racket (provide (contract-out [deposit (-> positive? any)] ; 數量一直是正值 [balance (-> positive?)])) (define amount 0) (define (deposit a) (set! amount (+ amount a))) (define (balance) amount) ) (require 'bank-account) (deposit 5) (balance) ; => 5 ;; 客戶端嘗試存儲一個負值時會出錯 ;; (deposit -5) ; => deposit: contract violation ;; expected: positive? ;; given: -5 ;; more details.... ~~~ ## 進一步閱讀 想知道更多嗎? 嘗試?[Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看