如何比较结构内部的数据?

时间:2017-10-24 15:19:51

标签: data-structures structure racket

让我说我有这个:         (define-struct person(年龄性别代码))         (define-struct subject(人次))

    (define P1 (make-subject (make-person 19 'f     'OM29Q) (list 299 194 242 303 243)))
    (define P2 ...)))
    (define P3 ...)))
    ...

    (define subjects P1 P2 ... )

我想找出列表科目中一个人的最低年龄。

我无法弄清楚如何实现这样的事情。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您正在寻找的功能是argmin

(define-struct person (age sex code) #:transparent)
(define-struct subject (person times)  #:transparent)

(define P1 (make-subject (make-person 19 'f     'OM29Q) (list 299 194 242 303 243)))
(define P2 (make-subject (make-person 31 'f     'OM29Q) (list 299 194 242 303 243)))
(define P3 (make-subject (make-person 14 'f     'OM29Q) (list 299 194 242 303 243)))
(define P4 (make-subject (make-person 45 'f     'OM29Q) (list 299 194 242 303 243)))
(define P5 (make-subject (make-person 56 'f     'OM29Q) (list 299 194 242 303 243)))
(define P6 (make-subject (make-person 20 'f     'OM29Q) (list 299 194 242 303 243)))

(define subjects (list P1 P2 P3 P4 P5 P6))

(define (min-age l)
  (argmin (lambda (s) (person-age (subject-person s))) l))

> (min-age subjects)
(subject (person 14 'f 'OM29Q) '(299 194 242 303 243))
> (person-age (subject-person (min-age subjects)))
14