Lambda 如何找到相应的列表?
到目前为止,我已经找到了相应的课程,如下所示:Lambda 如何找到相应的列表?,lambda,racket,Lambda,Racket,到目前为止,我已经找到了相应的课程,如下所示: (define-struct course (subject number title)) ;; A Course is a (make-course Sym Nat Str) ;; where: ;; subject is the subject of the course ;; number is the course number of the course ;; title is the of the course (define-str
(define-struct course (subject number title))
;; A Course is a (make-course Sym Nat Str)
;; where: ;; subject is the subject of the course
;; number is the course number of the course
;; title is the of the course
(define-struct student (id name age courses))
;; A Student is a (make-student Nat Str Nat (listof Course))
;; where: ;; id is the student id for the student
;; name is the name of the student
;; age is the age of the student
;; courses is a list, with one element for each different course
;; the students are enrolled in
(define math135 (make-course 'MATH 135 "Algebra"))
(define chem101 (make-course 'CHEM 101 "Intro to chemistry"))
(define eco638 (make-course 'ECO 638 "Econ"))
(define arts392 (make-course 'ARTS 392 "Arts"))
(define cs101 (make-course 'CS 101 "Intro to CS"))
(define stu1 (make-student 373647 "Mary" 40 (list math135 eco638)))
(define stu2 (make-student 367339 "Tony" 21 (list chem101 cs101 math135)))
(define stu3 (make-student 362927 "Joy" 35 (list chem101 arts392)))
(define students (list stu1 stu2 stu3))
那么我应该怎么做才能产生相应的学生名单呢?
比如说,
(filter (lambda (lst)
(and (symbol? coursesub (course-subject lst))
(equal? coursenum (course-number lst)))) los)
筛选那些课程列表中的课程与
cname
,cnum
匹配的学生
(check-expect (students-in-course 'MATH 135 students) (list stu1 stu2))
编辑:版本2(无本地):
编辑:版本3(无助手):
谢谢!但是,如果不允许使用局部递归或显式递归,那么我们应该怎么做?对于前者,请参见编辑。我不确定你所说的“显式递归”是什么意思,这里没有递归调用。很抱歉我解释得不清楚。实际上,对于这个问题,不允许同时使用命名的helper函数,也就是说,我们只能使用lambda和抽象列表函数。我只是不知道在我确定了相应的课程列表之后(你在课程匹配中做了什么?),下一步我应该做什么。谢谢你的耐心!替换对
课程匹配的调用?
它的主体应该会这样做。请参阅编辑。我认为stackoverflow中已经有类似的问题(搜索结构)。
; Symbol Number [List-of Student] -> [List-of Student]
(define (students-in-course.v2 coursesub coursenum lst)
(local ((define (course-match? course)
(and (symbol=? (course-subject course) coursesub)
(= (course-number course) coursenum))))
(filter (λ (student) (ormap course-match? (student-courses student))) lst)))
; Symbol Number Course -> Boolean
(define (course-match? coursesub coursenum course)
(and (symbol=? (course-subject course) coursesub)
(= (course-number course) coursenum)))
; Symbol Number [List-of Student] -> [List-of Student]
(define (students-in-course.v2 coursesub coursenum lst)
(filter (λ (student) (ormap (λ (course) (course-match? coursesub coursenum course))
(student-courses student)))
lst))
; Symbol Number [List-of Student] -> [List-of Student]
(define (students-in-course.v2 coursesub coursenum lst)
(filter (λ (student)
(ormap (λ (course)
(and (symbol=? (course-subject course) coursesub)
(= (course-number course) coursenum)))
(student-courses student)))
lst))