<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] ### 描述函數信息 ``` ;; profit : number -> number ;;給定票價,收益是收入和支出之差 ;;預期值 (profit 10) ->20 ;模板: ;(define (profit ticket-price) ...) (define (profit ticket-price) (- 20 ticket-price)) ``` ### 設計訣竅 - 如設計處理混合數據的函數的例子 ![E91219E4-C903-4B09-AFE2-D97989E47FB0.png](http://yanxuan.nosdn.127.net/4bab9f5981a93cda99cda8f4a023eee8.png) :-: 處理自應用數據的函數設計 #### 實例1:非遞歸案例 用 cond 來區別不同的數據類型 ``` ;數據定義: (define-struct circle (center radius)) (define-struct square (nw length) ;; shape是下列二者之 ;; 1.結構體:(make- circle p s) ;; 其中 p 是 posn 結構體,s是數;或 ;; 2.結構體:(make- square p s) ;; 其中 p 是 posn 結構體,s 是數 ;; 例子: 參見測試 ;; 模板: ;;(define (f a-shape) ;; (cond ;; [(square? a-shape) ...] ;; [(circle? a-shape) ...])) ;; 定義 (define (perimeter a-shape) (cond [(circle? a-shape) (* (* 2 (circle-radius a-shape)) pi)] [(square? a-shape) (* (square-length a-shape) 4)])) ;; 測試:(即例子) (= (perimter (make-square ... 3)) 12) (= (perimter (make-square ... 1)) (* 2 pi)) ``` #### 實例2:遞歸案例 開發函數 occurs1,該函數讀入一個網頁和一個符號,返回該符號在網頁中出現的次數,忽略 嵌入的網頁 ``` ;; occurs1 : sym,wp->number ;; 讀入一個網頁和一個符號,返回該符號在網頁中出現的次數,忽略嵌入的網頁 ;; 例子 ;;(= (occurs1 'a empty) 0) ;;(= (occurs1 'a (list 'a)) 1) ;;(= (occurs1 'a (list 'a (list 'a))) 1) ;;(= (occurs1 'a (list 'b 'a)) 1) ;; 模版 ;;(define (occurs1 sym wp) ;; (cond ;; [(empty? wp) ...] ;; [(cons? (first wp)) ...] ;; [(eq? sym (fits wp)) ... (first wp) ... (occurs1 sym (rest wp))] ;; [else ...(occurs1 sym (rest wp))...])) (define (occurs1 sym wp) (cond [(empty? wp) 0] [(cons? (first wp)) 0] [(eq? sym (first wp)) (+ 1 (occurs1 sym (rest wp)))] [else (occurs1 sym (rest wp))])) ;;測試 (= (occurs1 'a empty) 0) (= (occurs1 'a (list 'a)) 1) (= (occurs1 'a (list 'a (list 'a))) 1) (= (occurs1 'a (list 'b 'a)) 1) ``` 開發函數 occurs2,該函數類似于 occurs1,但是它計算該符號所有的出現次數,包括在嵌入網頁中的 出現 ``` ;; occurs2 : sym,wp->number ;; 但是它計算該符號所有的出現次數,包括在嵌入網頁中的出現 ;; 例子 ;;(= (occurs2 'a empty) 0) ;;(= (occurs2 'a (list 'a)) 1) ;;(= (occurs2 'a (list 'a (list 'a))) 2) ;;(= (occurs2 'a (list 'b 'a)) 1) ;; 模版 ;;(define (occurs2 sym wp) ;; (cond ;; [(empty? wp) ...] ;; [(cons? (first wp)) ... (occurs2 sym (first wp)) ... (occurs2 sym (rest wp))...] ;; [(eq? sym (first wp)) ... (first wp) ... (occurs2 sym (rest wp))...] ;; [else ...(occurs2 sym (rest wp))...])) (define (occurs2 sym wp) (cond [(empty? wp) 0] [(cons? (first wp)) (+ (occurs2 sym (first wp)) (occurs2 sym (rest wp)))] [(eq? sym (first wp)) (+ 1 (occurs2 sym (rest wp)))] [else (+ 0 (occurs2 sym (rest wp)))])) ;;測試 (= (occurs2 'a empty) 0) (= (occurs2 'a (list 'a)) 1) (= (occurs2 'a (list 'a (list 'a))) 2) (= (occurs2 'a (list 'b 'a)) 1) ```
                  <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>

                              哎呀哎呀视频在线观看