首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Drracket:创建一个函数,它将返回一个使用冒泡排序的比较器的排序列表

Drracket:创建一个函数,它将返回一个使用冒泡排序的比较器的排序列表
EN

Stack Overflow用户
提问于 2020-11-09 15:47:40
回答 1查看 53关注 0票数 0

这就是我要对字符串进行排序的内容:

代码语言:javascript
复制
;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))
      )
  )

这适用于按从A到Z的顺序对字符串进行排序

这就是我到目前为止的通用排序(func表示比较器:<,> =,string

代码语言:javascript
复制
;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)
      )
  )
EN

回答 1

Stack Overflow用户

发布于 2020-11-09 22:54:31

由于过于宽松的签名,您在这里没有正确使用您的抽象。从技术上讲,您希望最终的冒泡排序是这样工作的:

代码语言:javascript
复制
; general-bubble-sort: (X) [List-of X] [X X -> Boolean] -> [List-of X]

通过限制我们接收的内容的类型,并告诉用户他们应该如何使用它,我们消除了需要在general-sorted?中执行的检查。我们只知道我们必须将正确的函数传递给它,否则就是用户的错,因为没有传递正确的函数。您还可以在两个函数之间抽象更多的代码。如下所示:

代码语言:javascript
复制
(define (general-sorted? l func)
  (or (<= (length l) 1) (and (func (car l) (cadr l)) (general-sorted? (cdr l)))))

你的第二个函数有很多功能,让我们结合一些情况,也不要使用(equal? x boolean)。这是一种糟糕的做法。相反,我们应该在适当的时候使用布尔表达式来获取true。我们还应该在这里使用累加器,因为它清楚地说明了我们在向下递归时跟踪的是哪一块数据:

代码语言:javascript
复制
(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)))))

最后的冒泡排序函数不会改变。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64747587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档