【问题标题】:Scheme full-house poke function方案全屋戳功能
【发布时间】:2015-05-07 01:48:53
【问题描述】:

我正在尝试在 Scheme 中定义一个函数,该函数确定一个五元素列表是否包含一个完整的房子(即,分别有 3 个元素相同,另外 2 个元素相同)。尽管我把语法弄乱了,但我脑子里已经有了大纲。我正在使用 and, let, 来尝试这样做。输入是一个列表(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)))))

感谢您的帮助

【问题讨论】:

    标签: list scheme lisp racket


    【解决方案1】:

    你很接近 - 但首先要确保你了解 什么是满屋子,以及在这种情况下使用布尔连接器的正确方法。试试这个:

    (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)))))))
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多