使用向量、条件和结构

Using vectors, conds and structs

我正在创建一个结构体,它将显示酒店的楼层列表(例如 1 楼、2 楼)以及每层楼的空房间数。我创建了一个向量,允许用户编辑房间是 "empty" 还是 "full"。我遇到的唯一问题是我不知道如何创建代码来显示我可以进入我的结构的每层楼的空闲房间数量。我猜使用 cond 会起作用,但我真的不知道怎么写。

这是我目前所做的:

#lang racket
(define floor-one (make-vector 10))

(vector-set! floor-one 0 "empty")
(vector-set! floor-one 1 "full")
(vector-set! floor-one 2 "empty")
(vector-set! floor-one 3 "empty")
(vector-set! floor-one 4 "empty")
(vector-set! floor-one 5 "full")
(vector-set! floor-one 6 "full")
(vector-set! floor-one 7 "empty")
(vector-set! floor-one 8 "full")
(vector-set! floor-one 9 "full")

(define floor-two (make-vector 10))

(vector-set! floor-two 0 "empty")
(vector-set! floor-two 1 "full")
(vector-set! floor-two 2 "empty")
(vector-set! floor-two 3 "empty")
(vector-set! floor-two 4 "empty")
(vector-set! floor-two 5 "full")
(vector-set! floor-two 6 "full")
(vector-set! floor-two 7 "empty")
(vector-set! floor-two 8 "full")
(vector-set! floor-two 9 "full")

(struct hotel-rooms (floor rooms-empty)) 


(define empty-rooms1 (λ (any->vector x)
                   (cond
                     ())))

任务有一个完美的功能:vector-count。试试这个:

(define empty-rooms
  (λ (vec)
    (vector-count (λ (floor) (string=? floor "empty"))
                  vec)))

或者更简单:

(define (empty-rooms vec)
  (vector-count (curry string=? "empty") vec))

例如:

(empty-rooms floor-one)
=> 5