Racket 如何在球拍中使用贴图和过滤器

Racket 如何在球拍中使用贴图和过滤器,racket,Racket,我在尝试使用Racket中的地图和过滤器时遇到问题,原因如下: 我用一个结构来表示一个数据表,我知道我需要将该表交给一个映射函数进行迭代,然后过滤出非硬工作项以获得最终列表,但我不知道如何从我的结构中获得我需要的信息,也不知道映射和过滤器应该是什么样子 这就是我所拥有的: #lang racket (struct worker (name work study ent)) (define workers(list (worker '(bill) '

我在尝试使用Racket中的地图和过滤器时遇到问题,原因如下:

我用一个结构来表示一个数据表,我知道我需要将该表交给一个映射函数进行迭代,然后过滤出非硬工作项以获得最终列表,但我不知道如何从我的结构中获得我需要的信息,也不知道映射和过滤器应该是什么样子

这就是我所拥有的:

#lang racket

(struct worker (name work study ent))

(define workers(list
                     (worker '(bill) '(none) '(medium) '(none))
                     (worker '(jill) '(high) '(low) '(medium))
                     (worker '(tim) '(vhigh) '(none) '(vhigh))
                     (worker '(gary) '(medium) '(high) '(medium))
                     (worker '(samantha) '(vlow) '(vlow) '(medium))
                     (worker '(holly) '(vlow) '(low) '(low))
                     (worker '(ryan) '(low) '(low) '(low))
                     (worker '(quin) '(low) '(medium) '(vlow))
                     (worker '(lisa) '(medium) '(vlow) '(high))
                     (worker '(jennifer) '(low) '(low) '(vlow))
                     (worker '(jeff) '(high) '(low) '(high))
                     (worker '(george) '(medium) '(vhigh) '(medium))
                     (worker '(beth) '(none) '(none) '(low))
                     (worker '(maria) '(vlow) '(medium) '(low))
                     (worker '(simon) '(medium) '(high) '(high))
                     ))

(define convert
  (lambda (input)
    (match (input
           ('none 0)
           ('vlow 1)
           ('low 2)
           ('medium 3)
           ('high 4)
           ('vhigh 5)
           )
          )
    )
)

(define (hardworker workers)
  (map(lambda(workers)...
感谢您的帮助

#lang racket
(define-struct worker (name work study ent) #:transparent #:mutable)

(define workers-list
  (list
   (make-worker 'bill 'none 'medium 'none)
   (make-worker 'jill 'high 'low 'medium)
   (make-worker 'tim 'vhigh 'none 'vhigh)
   (make-worker 'gary 'medium 'high 'medium)))
; work hard
(filter (lambda (w) (equal? 'high (worker-work w))) workers-list)
; not wrok hard
(filter (lambda (w) (not (equal? 'high (worker-work w)))) workers-list)

代表工人 请注意,由于您正在放置
”(…)
(带引号的括号),因此每个字段都是
[Listof Symbol]
,而不是答案中表示的符号本身。每个结构都会产生“选择器函数”,可以提取字段的值

(worker-ent (worker '(quin) '(low) '(medium) '(vlow))) 
; => '(vlow). 

(first '(vlow)) 
; => 'vlow. 
如果我们仅使用符号来表示结构的字段,那么它将是:

(define workers
  (list   ; name     work    study   ent
   (worker 'bill     'none   'medium 'none)
   (worker 'jill     'high   'low    'medium)
   (worker 'tim      'vhigh  'none   'vhigh)
   (worker 'gary     'medium 'high   'medium)
   (worker 'samantha 'vlow   'vlow   'medium)
   (worker 'holly    'vlow   'low    'low)
   (worker 'ryan     'low    'low    'low)
   (worker 'quin     'low    'medium 'vlow)
   (worker 'lisa     'medium 'vlow   'high)
   (worker 'jennifer 'low    'low    'vlow)
   (worker 'jeff     'high   'low    'high)
   (worker 'george   'medium 'vhigh  'medium)
   (worker 'beth     'none   'none   'low)
   (worker 'maria    'vlow   'medium 'low)
   (worker 'simon    'medium 'high   'high)))
事实上,写下每个字段所代表的内容及其类型是明智的:

(struct worker (name work study ent) #:transparent)
; A Worker is a (worker Symbol Amount Amount Amount)
; interpretation. name is the name of the worker, work, study and ent are
; the Amount that represents the work, study and entertainment done by the worker. 

; Amount is one of
; - 'none
; - 'vlow
; - 'low
; - 'medium
; - 'high
; - 'vhigh
; interpretation. represents amount of something done. 
转换函数
convert
将“Amount”枚举放入一个顺序中,但是用于
match
的语法有问题。以下是测试的完整定义:

; convert : Amount -> Natural
; The ordering number of each Amount.
(define convert
  (lambda (input)
    (match input
      ['none   0]
      ['vlow   1]
      ['low    2]
      ['medium 3]
      ['high   4]
      ['vhigh  5])))

(check-equal? (convert 'none)   0)
(check-equal? (convert 'vlow)   1)
(check-equal? (convert 'low)    2)
(check-equal? (convert 'medium) 3)
(check-equal? (convert 'high)   4)
(check-equal? (convert 'vhigh)  5)
注1:我们可以通过使用
索引来避免使用match,如下所示:

(define (convert.v2 i)
  (index-of '(none  vlow low medium high vhigh) i))

(check-equal? (convert.v2 'none)   0)
(check-equal? (convert.v2 'vlow)   1)
(check-equal? (convert.v2 'low)    2)
(check-equal? (convert.v2 'medium) 3)
(check-equal? (convert.v2 'high)   4)
(check-equal? (convert.v2 'vhigh)  5)
数量谓词 为了解决这个问题,我们需要知道“至少高”和“不超过中等”的数量意味着什么,因此我们做出以下谓词:

; at-least-high? : Amount -> Boolean
; is `a` at least High?
(define (at-least-high? a)
  (>= (convert a) 4))

(check-false (at-least-high? 'none))
(check-false (at-least-high? 'vlow))
(check-false (at-least-high? 'low))
(check-false (at-least-high? 'medium))
(check-true  (at-least-high? 'high))
(check-true  (at-least-high? 'vhigh))

苦工?谓语 对一个人来说,努力工作意味着什么?这只是已经定义的两个谓词的连接:

; hardworker? : Worker -> Boolean
; Hard workers do at least high amount of work and
; have no more than medium amount of entertainment.
(define (hardworker? w)
  (and (at-least-high? (worker-work w)) 
       (no-more-than-medium? (worker-ent w))))

(check-true  (worker 'bill 'none 'medium 'none))
(check-false (worker 'jill 'high 'low    'medium))
注3:如果我有你对工人的定义,我必须先用
(工人工作w)
(工人输入w)
包装起来,以便从列表中提取符号

最后,可以使用
hardwarker?
workers
列表进行
筛选

注4:我们不需要映射来迭代列表,而是在幕后对列表进行过滤递归

; hardworker : [List-of Worker] -> [List-of Worker]
; Only keep hard-workers. 
(define (hardworker workers)
  (filter hardworker? workers))

(check-equal? (hardworker workers) (list (worker 'jill 'high 'low 'medium)))
(check-equal? (hardworker (rest (rest workers))) empty)
小版本 我们可以使用lambda将所有内容整合到一个函数中:

(define (hardworker.v2 workers)
  (filter (λ (w) (and (member (worker-work w) '(high vhigh)) 
                      (member (worker-ent w)  '(none vlow low medium))))
          workers))
代表工人 请注意,由于您正在放置
”(…)
(带引号的括号),因此每个字段都是
[Listof Symbol]
,而不是答案中表示的符号本身。每个结构都会产生“选择器函数”,可以提取字段的值

(worker-ent (worker '(quin) '(low) '(medium) '(vlow))) 
; => '(vlow). 

(first '(vlow)) 
; => 'vlow. 
如果我们仅使用符号来表示结构的字段,那么它将是:

(define workers
  (list   ; name     work    study   ent
   (worker 'bill     'none   'medium 'none)
   (worker 'jill     'high   'low    'medium)
   (worker 'tim      'vhigh  'none   'vhigh)
   (worker 'gary     'medium 'high   'medium)
   (worker 'samantha 'vlow   'vlow   'medium)
   (worker 'holly    'vlow   'low    'low)
   (worker 'ryan     'low    'low    'low)
   (worker 'quin     'low    'medium 'vlow)
   (worker 'lisa     'medium 'vlow   'high)
   (worker 'jennifer 'low    'low    'vlow)
   (worker 'jeff     'high   'low    'high)
   (worker 'george   'medium 'vhigh  'medium)
   (worker 'beth     'none   'none   'low)
   (worker 'maria    'vlow   'medium 'low)
   (worker 'simon    'medium 'high   'high)))
事实上,写下每个字段所代表的内容及其类型是明智的:

(struct worker (name work study ent) #:transparent)
; A Worker is a (worker Symbol Amount Amount Amount)
; interpretation. name is the name of the worker, work, study and ent are
; the Amount that represents the work, study and entertainment done by the worker. 

; Amount is one of
; - 'none
; - 'vlow
; - 'low
; - 'medium
; - 'high
; - 'vhigh
; interpretation. represents amount of something done. 
转换函数
convert
将“Amount”枚举放入一个顺序中,但是用于
match
的语法有问题。以下是测试的完整定义:

; convert : Amount -> Natural
; The ordering number of each Amount.
(define convert
  (lambda (input)
    (match input
      ['none   0]
      ['vlow   1]
      ['low    2]
      ['medium 3]
      ['high   4]
      ['vhigh  5])))

(check-equal? (convert 'none)   0)
(check-equal? (convert 'vlow)   1)
(check-equal? (convert 'low)    2)
(check-equal? (convert 'medium) 3)
(check-equal? (convert 'high)   4)
(check-equal? (convert 'vhigh)  5)
注1:我们可以通过使用
索引来避免使用match,如下所示:

(define (convert.v2 i)
  (index-of '(none  vlow low medium high vhigh) i))

(check-equal? (convert.v2 'none)   0)
(check-equal? (convert.v2 'vlow)   1)
(check-equal? (convert.v2 'low)    2)
(check-equal? (convert.v2 'medium) 3)
(check-equal? (convert.v2 'high)   4)
(check-equal? (convert.v2 'vhigh)  5)
数量谓词 为了解决这个问题,我们需要知道“至少高”和“不超过中等”的数量意味着什么,因此我们做出以下谓词:

; at-least-high? : Amount -> Boolean
; is `a` at least High?
(define (at-least-high? a)
  (>= (convert a) 4))

(check-false (at-least-high? 'none))
(check-false (at-least-high? 'vlow))
(check-false (at-least-high? 'low))
(check-false (at-least-high? 'medium))
(check-true  (at-least-high? 'high))
(check-true  (at-least-high? 'vhigh))

苦工?谓语 对一个人来说,努力工作意味着什么?这只是已经定义的两个谓词的连接:

; hardworker? : Worker -> Boolean
; Hard workers do at least high amount of work and
; have no more than medium amount of entertainment.
(define (hardworker? w)
  (and (at-least-high? (worker-work w)) 
       (no-more-than-medium? (worker-ent w))))

(check-true  (worker 'bill 'none 'medium 'none))
(check-false (worker 'jill 'high 'low    'medium))
注3:如果我有你对工人的定义,我必须先用
(工人工作w)
(工人输入w)
包装起来,以便从列表中提取符号

最后,可以使用
hardwarker?
workers
列表进行
筛选

注4:我们不需要映射来迭代列表,而是在幕后对列表进行过滤递归

; hardworker : [List-of Worker] -> [List-of Worker]
; Only keep hard-workers. 
(define (hardworker workers)
  (filter hardworker? workers))

(check-equal? (hardworker workers) (list (worker 'jill 'high 'low 'medium)))
(check-equal? (hardworker (rest (rest workers))) empty)
小版本 我们可以使用lambda将所有内容整合到一个函数中:

(define (hardworker.v2 workers)
  (filter (λ (w) (and (member (worker-work w) '(high vhigh)) 
                      (member (worker-ent w)  '(none vlow low medium))))
          workers))

我希望我能给你更多+1。这非常清楚,谢谢你的笔记。我希望我能给你更多+1。这非常清楚,谢谢你的笔记。