如何在 Racket 中重新排列配对列表(具有字符或数字)的顺序?

发布于 2025-01-13 08:36:09 字数 244 浏览 0 评论 0原文

如何在 Racket 中将所有带有“A 'K 'Q 'J”字符的牌移动到我的手牌前面,并将所有数字 1-10 移动到最后?

例如: ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♠) (A . ♠))

变为

((A . ♠) (Q . ♠) (10 . ♠) (9 . ♣ ) (4 . ♠))

我在这里遇到的困难是处理数字和字符的混合。我可以使用排序函数对数字进行排序,但是一旦出现字符就会出现错误。

How can I move all cards with characters 'A 'K 'Q 'J to the front of my hand and all the numbers 1-10 to the end in Racket?

Ex: ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠))

becomes

((A . ♠) (Q . ♦) (10 . ♠) (9 . ♣) (4 . ♠))

My struggle here is dealing with the mixture of numbers and char. I can sort the numbers with the sort function but I get errors as soon as a char appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2025-01-20 08:36:09

基本上 sort 有一个可选的 less-than? 函数,您可以在其中检查第一个参数是否小于第二个参数。例如。想象一下,我想按卡片的值 1-10 对卡片进行排序,而 J、Q、K、A 是 11-14:

;; produce a numeric value for all cards
(define (card->value c)
  (let ((v (car c)))
    (case v
      ((J) 11)
      ((Q) 12)
      ((K) 13)
      ((A) 14)
      (else v))))

(define cards '((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠)))
(map card->value cards) ; ==>  (4 9 10 12 14)

(define (card-less? c1 c2)
   (< (card->value c1) (card->value c2)))

(sort '((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠)) card-less?)
; ==> ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠))

由于您遵循相反的顺序,因此您应该传递 card-greater? 而不是card-less?sort 的相反会颠倒顺序吗?

如果您使用#langracket,您可以利用两个额外的可选值并使其无需无卡?

(sort cards < #:key card->value     #:cache-keys? #t)
; ==> ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠))

Basically sort has an optional less-than? function you can provide where you check if first argument is less than second. eg. Imagine I want to sort cards by their value 1-10 and that J,Q,K,A are 11-14:

;; produce a numeric value for all cards
(define (card->value c)
  (let ((v (car c)))
    (case v
      ((J) 11)
      ((Q) 12)
      ((K) 13)
      ((A) 14)
      (else v))))

(define cards '((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠)))
(map card->value cards) ; ==>  (4 9 10 12 14)

(define (card-less? c1 c2)
   (< (card->value c1) (card->value c2)))

(sort '((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠)) card-less?)
; ==> ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠))

Since you are after the opposite order you should pass a card-greater? instead that does the opposite of card-less? and sort will reverse the order.

If you are using #lang racket you can make use of the two extra optional values and make it without card-less?:

(sort cards < #:key card->value     #:cache-keys? #t)
; ==> ((4 . ♠) (9 . ♣) (10 . ♠) (Q . ♦) (A . ♠))
萌逼全场 2025-01-20 08:36:09

您可以使用排序功能,例如这个:(排序顺序过程)过程可能如下所示:

(define sort/cards
  (lambda(a b)
    (if (check: a is member of the list '(A K Q J))
       true
       false)))

您可以开发(check ...)函数并删除(if ...),因为检查将返回适合排序的值。

如果你想进一步对字母进行排序,你需要在check中比较ab

You use the sort function like this: (sort sequence procedure). The procedure could look like this:

(define sort/cards
  (lambda(a b)
    (if (check: a is member of the list '(A K Q J))
       true
       false)))

You can develop the (check ...) function and also drop the (if ...), as checking will return the value fitted for sort.

If you want to further sort the letters, you need to compare a and b within check.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文