Let's say I have a struct like this:

(struct house (bedrooms bathrooms address price))

How can I create a sub-struct for, say, houses with 3 bedrooms and 2 bathrooms?

(struct 3-2-house house
   ; ???
   ; somehow fill in bedrooms=3 and bathrooms=2
   ; ???

(define house-on-apple-street (3-2-house "123 apple street" 100000))

It seems something like #:auto is called for here but it's not clear how to apply #:auto to fields inherited from parent struct ...


1 Answers

Community On

When you define a struct you can create struct by make-structname. After define struct you can use selector.

;;; struct name is house
(define-struct house (bedrooms bathrooms label address price))
;;; selector
;;; house-bedrooms house-bathrooms ... house-price

;;; make struct
(define house-on-apple-street (make-house 3 2 "123 apple street" (list 1 2 3) 100000))

(struct? house-on-apple-street)

You can make struct in struct (family tree)

(define-struct house-tree (neighbor1 neighbor2 bedrooms bathrooms price))
(define example
  (make-house-tree empty
                   (make-house-tree empty empty 1 1 100)
                   2 3 200))
(struct? example)

;;; selector
(house-tree-neighbor2 example)