<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] > [手冊](https://deathking.github.io/yast-cn/contents/preface.html) ## 計算 ``` (- 10 3) ;→ 7 (- 10 3 5) ;→ 2 (* 2 3) ;→ 6 (* 2 3 4) ;→ 24 (/ 29 3) ;→ 29/3 (/ 29 3 7) ;→ 29/21 (/ 9 6) ;→ 3/2 (exact->inexact (/ 29 3 7)) ;→ 1.380952380952381 //把分數轉為浮點數 ``` ## quote引用 `(+ 2 3)`返回5,然而`(quote (+ 2 3))`則向程序返回`(+ 2 3)`本身。因為quote的使用頻率很高,他被簡寫為’。 ``` ’(+ 2 3) 代表列表 (+ 2 3) 本身 `’+ 代表符號 + 本身; ``` ## 定義函數 ### 無參函數 (即全局變量) hello.rkt ``` ; Hello world as a variable (define vhello "Hello world") ;1 ; Hello world as a function (define fhello (lambda () ;2 "Hello world")) ``` `cmd`到文件所在目錄,執行`racket` ``` (load "hello.rkt") vhello ;"Hello world" fhello ;Value 16: #[compound-procedure 16 fhello] (fhello) ;Value 17: "Hello world" ``` ### 有參函數 farg.skt ``` ; hello with name (define hello (lambda (name) (string-append "Hello " name "!"))) ; sum of three numbers (define sum3 (lambda (a b c) (+ a b c))) ``` 調用 ``` (load "farg.scm") (hello "Lucy") ;Value 20: "Hello Lucy!" (sum3 10 20 30) ;Value: 60 Hello ``` ### 一種函數定義的短形式 ``` ; hello with name (define (hello name) (string-append "Hello " name "!")) ; sum of three numbers (define (sum3 a b c) (+ a b c)) ``` ## 局部變量 ### let表達式 格式 變量的作用域(Scope)為`body`體 ``` (let binds body) [binds] → ((p1 v1) (p2 v2) ...) ``` demo ``` (let ([x 1] [y 2]) (+ x y)) ``` 嵌套 ``` (let ((i 1)) (let ((j (+ i 2))) (* i j))) ;Value: 3 ``` let*表達式可以用于引用定義在同一個綁定中的變量。實際上,let*只是嵌套的let表達式的語法糖而已。 ``` (let ((i 1) (j (+ i 2))) (* i j)) ;Error ;;;======== (let* ((i 1) (j (+ i 2))) (* i j)) ;Value: 3 ``` 實際上,let表達式只是lambda表達式的一個語法糖: ``` (let ((p1 v1) (p2 v2) ...) exp1 exp2 ...) ;? ((lambda (p1 p2 ...) exp1 exp2 ...) v1 v2) ``` ## 重復 ### 遞歸 ``` (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) ;;; (fact 5) => 5*4*3*2*1 ``` ### 尾遞歸 ``` (define (fact-tail n) (fact-rec n n)) (define (fact-rec n p) (if (= n 1) p (let ((m (- n 1))) (fact-rec m (* p m))))) ;;; (fact-tail 5) ? (fact-rec 5 5) ? (fact-rec 4 20) ? (fact-rec 3 60) ? (fact-rec 2 120) ? (fact-rec 1 120) ? 120 ``` ## 高階函數 ### 排序 高階函數(Higher Order Function)是一種以函數為參數的函數。它們都被用于映射(mapping)、過濾 (filtering)、歸檔(folding)和排序(sorting)表 排序demo ``` ;;;常規排序 (sort '(7883 9099 6729 2828 7754 4179 5340 2644 2958 2239) <) ;? (2239 2644 2828 2958 4179 5340 6729 7754 7883 9099) ;;; 按后兩位進行排序 (sort '(7883 9099 6729 2828 7754 4179 5340 2644 2958 2239) (lambda (x y) (< (modulo x 100) (modulo y 100)))) ;? (2828 6729 2239 5340 2644 7754 2958 4179 7883 9099) ``` ### map 格式:`(map procedure list1 list2 ...) ` ``` (map + '(1 2 3) '(4 5 6)) ;? (5 7 9) (map (lambda (x) (* x x)) '(1 2 3)) ;? (1 4 9) ``` ### for-each ``` for-each的格式與map一致。但for-each并不返回一個具體的值,只是用于副作用。 (define sum 0) (for-each (lambda (x) (set! sum (+ sum x))) '(1 2 3 4)) sum ;? 10 ``` ### 過濾 ``` (keep-matching-items '(1 2 -3 -4 5) positive?) ;? (1 2 5) ``` ### apply函數 將表展開,作為過程的參數 ``` (apply max '(1 3 2)) ;? 3 (apply + 1 2 '(3 4 5)) ;? 15 (apply - 100 '(5 12 17)) ;? 66 ``` ## 輸入輸出 函數(open-input-file filename)可以用于打開一個文件 函數(read-char port)用于從端口中讀取一個字符 當讀取到文件結尾(EOF)時,此函數返回eof-object,你可以使用eof-object?來檢查 函數(close-input-port port)用于關閉輸入端口 ``` (define (read-file file-name) (let ((p (open-input-file file-name))) (let loop((ls1 '()) (c (read-char p))) (if (eof-object? c) (begin (close-input-port p) (list->string (reverse ls1))) (loop (cons c ls1) (read-char p)))))) ``` ``` (read-file "hello.txt") ``` ## 賦值 ### set! ``` (define var 1) ;賦值前參數應被定義 (set! var (* var 10)) var ; 10 ``` ### 賦值和內部狀態 ``` (define bank-account (let ((balance 10)) (lambda (n) (set! balance (+ balance n)) balance))) ;balance 為body中操作,請查看let 的語法說明 ``` ``` (gates-bank-account 50) ;60 (gates-bank-account -55) ;5 ``` ### 表的破壞性操作(set-car!,set-cdr!) ``` (define tree '((1 2) (3 4 5) (6 7 8 9))) (set-car! (car tree) 100) ; ((100 2) (3 4 5) (6 7 8 9)) (set-cdr! (third tree) '(a b c)) ; ((100 2) (3 4 5) (6 a b c)) ``` ### 隊列 ... ## 關聯表和哈希表 ### 關聯表 關聯表是一個由序對組成的表,它是一個用于表達關聯的基本數據類型。 如果它們找到序對的 **car** 等于給定的`key` 符號,字符,和數字常被作為鍵使用,因為它們可以使用諸如eq?或者eqv?的快速比較函數被比較。 在作為鍵被使用前,字符串應該被轉換為符號,從而獲得更好的性能 函數`assq`,`assv`,和`assoc` 函數都可進行搜索 ,分別使用 `eq?`,`eqv?`,和`equal?`進行比較 ``` (define wc '((hi . 3) (everybody . 5) (nice . 3) (to . 10) (meet . 4) (you . 8))) (assq 'hi wc) ;? (hi . 3) (assq 'you wc) ;? (you . 8) (assq 'i wc) ? () (define n '((1 2 3) (4 5 6) (7 8 9))) (assv 1 n) ;? (1 2 3) (assv 8 n) ;? () ``` ### 哈希表 ``` (make-eq-hash-table size), (make-eqv-hash-table size), (make-equal-hash-table size), (make-string-hash-table size) ``` 分別使用eq?,eqv?,equal?,和string=?比較鍵的值 哈希表的初始大小(size)可以選擇性指定(optional) ## 定義語法 (宏) 宏的寫法與函數類似,但是猶豫函數的閉包性不能影響外部值. ``` (define-syntax add (syntax-rules () ((_ x) ;//_表示宏的名字 (set! x (+ x 1))))) ``` > 定義 `nil!` 宏.執行后把 定義的值復制為`'()` ``` (define a 2) (add a) a ;3 ``` ## 宏實現 while ``` (define-syntax while (syntax-rules () ((_ pred b1 ...) (let loop () (when pred b1 ... (loop)))))) ``` ``` (let ((i 0)) (while (< i 10) (display i) (display #\Space) (set! i (+ i 1)))) 0 1 2 3 4 5 6 7 8 9 ``` ### 多個定義模式 ``` (define-syntax incf (syntax-rules () ((_ x) (begin (set! x (+ x 1)) x)) ((_ x i) (begin (set! x (+ x i)) x)))) ``` ``` (let ((i 0) (j 0)) (incf i) (incf j 3) (display (list 'i '= i)) (newline) (display (list 'j '= j))) (i = 1) (j = 3) ``` ## error 錯誤處理 `error` 會中斷程序 ``` (define demo ( (error "err") (print "hello") )) (print "word") //輸出 //err ``` 案例:限制參數類型 ``` ;; checked-area-of-disk : scheme-value ->number ;; 如果 v 數的話,計算半徑為 v 的圓盤的面積 (define (checked-area-of-disk v) (cond [(number? v) v] [else (error 'checked-area-of-disk "number expected")] )) (checked-area-of-disk "12") ; checked-area-of-disk: number expected ```
                  <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>

                              哎呀哎呀视频在线观看