第一:预计非空列表

第一:预计非空列表

问题描述:

我一直得到错误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) 
+1

你”在进入第二个或第三个条件表达式之前,需要确保你总是有两个人。错误是告诉你你试图将空列表传递给'(first)' - 这种情况发生在'sub'只有一个成员的情况下。 – pdoherty926

你有一个类型不匹配:

(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的List。这意味着(first sub)here1subject

但后来你打电话idk它在here1(或here3),然后在here2调用(first sub)。此时subsubject,而不是一个列表。因此错误,

first:需要一个非空列表
给出:(make-subject (make-person 22 'm 'MW17K) (list 220 301 189 272 311))

我认为使用让有车,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))