2021年1月4日星期一

filter/get certain elements from a list of structs in DrRacket?

I want to get some elements into one list. for example:

(define-struct fruit (name price))

(list (make-fruit ('apple 2)) (make-fruit ('pear 3)) (make-fruit ('orange 4))) I want to get the list of all price (list 2 3 4)

I am only allowed to use map/filter/foldr/foldr . I don't know how to realize this, I guess map or filter is useful for this taks.

But I only know to to get list of prices via a function,

eg. (define (all-price f) (...(cons (fruit-price (first f))...(all-price (rest f))..))

https://stackoverflow.com/questions/65572690/filter-get-certain-elements-from-a-list-of-structs-in-drracket January 05, 2021 at 11:05AM

没有评论:

发表评论