方案满屋戳功能
Scheme full-house poke function
我正在尝试在 Scheme 中定义一个函数来确定一个五元素列表是否包含满屋(即分别有 3 个元素相同和另外 2 个元素相同)。我脑子里有大纲,虽然我弄乱了语法。我正在使用 and, let, to try and do this.输入是一个列表(5 个元素,编号 1-13),输出是一个布尔值。这是我目前所拥有的:
(define is-full-house?
(lambda (listy)
;; Sort listy from smallest to greatest
(let ((sorted-list (sort listy <=)))
(and
((= (first sorted-list) (second sorted-list)) (= (fourth sorted-list) (fifth sorted-list))))
(or
((= third fourth)) (= first third)))))
感谢帮助
您很接近 - 但请确保您首先了解 是什么 满堂彩,以及在这种情况下使用布尔连接符的正确方法。试试这个:
(define is-full-house?
(lambda (listy)
(let ((sorted-list (sort listy <=)))
(or
(and
(= (first sorted-list) (second sorted-list) (third sorted-list))
(= (fourth sorted-list) (fifth sorted-list)))
(and
(= (first sorted-list) (second sorted-list))
(= (third sorted-list) (fourth sorted-list) (fifth sorted-list)))))))
我正在尝试在 Scheme 中定义一个函数来确定一个五元素列表是否包含满屋(即分别有 3 个元素相同和另外 2 个元素相同)。我脑子里有大纲,虽然我弄乱了语法。我正在使用 and, let, to try and do this.输入是一个列表(5 个元素,编号 1-13),输出是一个布尔值。这是我目前所拥有的:
(define is-full-house?
(lambda (listy)
;; Sort listy from smallest to greatest
(let ((sorted-list (sort listy <=)))
(and
((= (first sorted-list) (second sorted-list)) (= (fourth sorted-list) (fifth sorted-list))))
(or
((= third fourth)) (= first third)))))
感谢帮助
您很接近 - 但请确保您首先了解 是什么 满堂彩,以及在这种情况下使用布尔连接符的正确方法。试试这个:
(define is-full-house?
(lambda (listy)
(let ((sorted-list (sort listy <=)))
(or
(and
(= (first sorted-list) (second sorted-list) (third sorted-list))
(= (fourth sorted-list) (fifth sorted-list)))
(and
(= (first sorted-list) (second sorted-list))
(= (third sorted-list) (fourth sorted-list) (fifth sorted-list)))))))