Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 获取一个值并将其传递给结构列表,然后返回具有相应值的列表_List_Struct_Lambda_Racket_Matching - Fatal编程技术网

List 获取一个值并将其传递给结构列表,然后返回具有相应值的列表

List 获取一个值并将其传递给结构列表,然后返回具有相应值的列表,list,struct,lambda,racket,matching,List,Struct,Lambda,Racket,Matching,我正在尝试编写一个函数,它以一年和一个结构列表(定义为事件)作为输入,并输出相应的结构 (define-struct incident (name day mon yr)#:transparent) (define cake (make-incident "cake" 15 "Apr" 2015)) (define Graduation (make-incident "graduation" 2 "Mar" 2017)) (define (incidentYr yr aList)

我正在尝试编写一个函数,它以一年和一个结构列表(定义为事件)作为输入,并输出相应的结构

(define-struct incident (name day mon yr)#:transparent)

(define cake (make-incident "cake" 15 "Apr" 2015))
(define Graduation (make-incident "graduation" 2 "Mar" 2017))

    (define (incidentYr yr aList)
  (foldl
   (lambda (x y) (if (equal? (incident-yr x) yr) (append x y) y))
   '()  aList))

(check-expect (incidentYr 2015 (list (incident "cake" 29 "Apr" 2015) (incident "graduation" 7 "Mar" 2017))) (list (incident "cake" 29 "Apr" 2015)))
但我得到的错误是:

    check-expect encountered the following error instead of the expected value, (list (incident "cake" 29 "Apr" 2015)). 
   :: append: expects a list, given (incident "cake" 29 "Apr" 2015)

似乎不起作用。

在文件夹中的lambda中,将
(追加x y)
更改为
(追加(列表x)y)
。您也可以将其更改为
(cons x y)

更自然的解决方案是使用过滤器而不是折叠:

(filter (λ (x) (= (incident-yr x) yr)) aList)

在foldl中的lambda中,将
(append x y)
更改为
(append(list x)y)
。您也可以将其更改为
(cons x y)

更自然的解决方案是使用过滤器而不是折叠:

(filter (λ (x) (= (incident-yr x) yr)) aList)