簡體   English   中英

Drracket:創建一個函數,該函數將使用冒泡排序返回給定比較器的排序列表

[英]Drracket: Create a function that will return a sorted list given a comparer using bubble sort

這是我必須對字符串進行排序的內容:

;create a function that checks if a list of strings is sorted
(define (stringCmpr l)
         (if (<= (length l) 1)
            true
            (and (string<=? (car l) (cadr l))(stringCmpr (cdr l))
                 )
            )
  )
;Create a funciton that checks if a list of numbers is sorted
(define (numCmpr l)
         (if (<= (length l) 1)
            true
            (and (<= (car l) (cadr l)) (numCmpr (cdr l))
                 )
            )
  )
;create function that checks whether a list containes numbers of 
strings checks if the list is sorted
(define (is-sorted? l)
         (if (number? (car l))
            (numCmpr l)
            (stringCmpr l)
            )
  )
    (define (bubble-pass lst)
      (cond
        ((empty? lst) lst)
        ((= (length lst) 1) lst)
        ((and (= (length lst) 2) (string>? (first lst) (second lst))) 
(list 
    (second lst) (first lst)))
        ((and (= (length lst) 2) (string<? (first lst) (second lst))) 
lst)
        ((string>? (first lst) (second lst))
         (append
          (list (second lst))
          (bubble-pass (append (list (first lst)) (rest (rest lst))))
          )
         )
        (else
         (append (list (first lst) (second lst)) (bubble-pass (rest 
(rest lst))))
         )
        )
      )

(define (string-bubble-sort lst)
  (if (is-sorted? lst)
      lst
      (string-bubble-sort (bubble-pass last))
      )
  )

這適用於從 AZ 按順序對字符串進行排序

這是我到目前為止的一般排序(func 表示比較器:<、> =、string<? 等):

;create a function that checks if a list of strings is sorted
(define (gen-stringCmpr l func)
         (if (<= (length l) 1)
            true
            (and (func (car l) (cadr l))(gen-stringCmpr (cdr l) func)
                 )
             )
  )
;Create a funciton that checks if a list of numbers is sorted
(define (gen-numCmpr l func)
         (if (<= (length l) 1)
            true
            (and (func (car l) (cadr l)) (gen-numCmpr (cdr l) func)
                 )
            )
  )
;create funciton that checks whether a list contains numbers or 
strings checks if the list is sorted
(define (general-sorted? l func)
         (if (number? (car l))
            (gen-numCmpr l func)
            (gen-stringCmpr l func)
            )
  )
; Purpose: Create a function that bubble sorts a list given a 
; comparison function

;Signature:
; list function-> list

;Examples:
(check-expect (general-bubble-sort (list "B" "A" "C") string<?) (list 
"A" "B" "C"))
(check-expect (general-bubble-sort (list "B" "A" "C") string>?) (list 
"C" "B" "A"))
(check-expect (general-bubble-sort (list 6 4 5) <) (list 4 5 6))
(check-expect (general-bubble-sort (list 2 3 1) >) (list 3 2 1))

Stub:
(define (general-bubble-sort lst func) '( "spinach")

 Template:

Code:

(define (general-bubble-pass lst func)
  (cond
    ((empty? lst) last)
    ((= (length lst) 1) last)
    ((and (= (length lst) 2) (equal? (func (first lst) (second lst)) 
false)) (list (second lst) (first lst)))
    ((and (= (length lst) 2) (func (first lst) (second lst))) last)
    ((equal? (func (first lst) (second lst)) false)
     (append
      (list (second last))
      (general-bubble-pass (append (list (first lst)) (rest (rest 
lst))) func)
      )
     )
    (else
     (append (list (first lst) (second lst)) (general-bubble-pass 
(rest (rest lst)) func))
     )
    )
  )

(define (general-bubble-sort lst func)
  (if (general-sorted? lst func)
      lst
      (general-bubble-sort (general-bubble-pass lst func) func)
      )
  )

由於過度寬松的簽名,您在這里沒有正確使用您的抽象。 從技術上講,您希望最終的冒泡排序像這樣工作:

; general-bubble-sort: (X) [List-of X] [X X -> Boolean] -> [List-of X]

通過限制我們接收的內容的類型並告訴用戶他們應該如何使用它,我們消除了我們需要在general-sorted?的檢查general-sorted? . 我們只知道我們必須將正確的函數傳遞給它,否則沒有傳遞正確的函數是用戶的錯。 您還可以在兩個函數之間抽象出更多代碼。 像這樣:

(define (general-sorted? l func)
  (or (<= (length l) 1) (and (func (car l) (cadr l)) (general-sorted? (cdr l)))))

你的第二個函數有很多事情要做,讓我們結合一些情況,也不要使用(equal? x boolean) 那是不好的做法。 相反,我們應該在適當的時候使用布爾表達式來使我們為true 我們還應該在這里使用累加器,因為它清楚地說明了我們在重復出現時正在跟蹤哪些數據:

(define (general-bubble-pass lst func)
  (local [(define (general-bubble-pass-acc lst bubble)
            (cond
              [(empty? lst) (list bubble)]
              [(not (func (first lst) bubble)) (cons bubble (general-bubble-pass-acc (rest lst) (first lst)))]
              [else (cons (first lst) (general-bubble-pass-acc (rest lst) bubble))]))]
    (if (<= (length lst) 1) lst (general-bubble-pass-acc (rest lst) (first lst)))))

您的最終冒泡排序功能不會改變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM