我在启动程序时始终收到错误first: expects a non-empty list
:(make-subject (make-person 22 'm 'MW17K) (list 220 301 189 272 311))
并且无法找到原因。
;;equals 25 but my code keeps erroring. please help
(person-age (subject-person (first sub)))
(define-struct person (age sex code))
(define-struct subject (person times))
(define VP01 (make-subject (make-person 22 'm 'MW17K) (list 220 301 189 272 311)))
(define VP02 (make-subject (make-person 25 'f 'MP25G) (list 234 197 253 257 206)))
(define VP03 (make-subject (make-person 23 'f 'CT03R) (list 197 202 214 222 233)))
(define VP04 (make-subject (make-person 20 'm 'MM09R) (list 273 314 257 264 217)))
(define VP05 (make-subject (make-person 19 'm 'KR22I) (list 198 197 228 253 199)))
(define VP06 (make-subject (make-person 26 'm 'FR01B) (list 212 204 289 294 223)))
(define VP07 (make-subject (make-person 28 'f 'RA15R) (list 258 323 189 247 303)))
(define VP08 (make-subject (make-person 22 'm 'RP18R) (list 221 307 182 271 316)))
(define VP09 (make-subject (make-person 24 'f 'GH31W) (list 230 295 304 264 237)))
(define VP10 (make-subject (make-person 19 'f 'OM29Q) (list 299 194 242 303 243)))
(define subjects (list VP01 VP02 VP03 VP04 VP05 VP06 VP07 VP08 VP09 VP10))
(define (idk sub)
(cond
[(empty? sub) empty]
[(< (person-age (subject-person (first sub)))
(person-age (subject-person (first (rest sub)))))
(idk (first sub))]
[else (idk (first (rest sub)))]))
(idk subjects)
答案 0 :(得分:1)
您的类型不匹配:
(define (idk sub)
(cond
[(empty? sub) empty]
[(< (person-age (subject-person (first sub))) ; here2 -------- NB
(person-age (subject-person (first (rest sub)))))
(idk (first sub))] ; here1 ------------------------ NB
[else (idk (first (rest sub)))])) ; here3
(idk subjects)
使用idk
列表调用 subject
,因此其定义中的sub
是subject
的列表。这意味着 here1 的(first sub)
为subject
。
然后你在 here1 (或 here3 )上用idk
调用它,然后在 here2 <{}调用(first sub)
< / em>的。此时sub
是subject
,而不是列表。因此错误,
first
:预计非空列表 给定:(make-subject (make-person 22 'm 'MW17K) (list 220 301 189 272 311))
答案 1 :(得分:0)
我认为使用let和car,cdr使代码更清晰:
(let loop ([loop_list subjects])
(if (>= (length loop_list) 2)
(if (>= (person-age (subject-person (car loop_list)))
(person-age (subject-person (cadr loop_list))))
(person-age (subject-person (car loop_list)))
(loop (cdr loop_list)))
empty))