Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
如何使用高阶函数重写这些(使用Dr.Racket)_Racket_Higher Order Functions - Fatal编程技术网

如何使用高阶函数重写这些(使用Dr.Racket)

如何使用高阶函数重写这些(使用Dr.Racket),racket,higher-order-functions,Racket,Higher Order Functions,这是我的家庭作业,但我们只允许使用过滤器、映射、foldr、排序、构建列表和lambda,而不是显式递归 如何使用上面的高阶函数重写这些函数,以避免函数本身调用 我现在拥有的是: (define (worthless loc name) (cond [(empty? loc) loc] [(equal? name (coin-name (first loc))) (cons (make-coin (coin-name (first loc)) 0) (worthless (r

这是我的家庭作业,但我们只允许使用过滤器、映射、foldr、排序、构建列表和lambda,而不是显式递归

如何使用上面的高阶函数重写这些函数,以避免函数本身调用

我现在拥有的是:

(define (worthless loc name)
  (cond
    [(empty? loc) loc]
    [(equal? name (coin-name (first loc))) (cons (make-coin (coin-name (first loc)) 0) (worthless (rest loc) name))]
    [else (cons (first loc) (worthless (rest loc) name))]))

(define (working-group locations group-tz)
  (cond
    [(empty? locations) empty]
    [(and (equal? (utc-hours group-tz) (utc-hours (location-timezone (first locations)))) (equal? (utc-sign group-tz) (utc-sign (location-timezone (first locations)))))
     (cons (location-city (first locations)) (working-group (rest locations) group-tz))]
    [(and (equal? (add1 (utc-hours group-tz)) (utc-hours (location-timezone (first locations))))
          (equal? (utc-sign group-tz) (utc-sign (location-timezone (first locations))))
          (equal? (utc-mins group-tz) (utc-mins (location-timezone (first locations)))))
     (cons (location-city (first locations)) (working-group (rest locations) group-tz))]
    [(and (equal? (sub1 (utc-hours group-tz)) (utc-hours (location-timezone (first locations))))
          (equal? (utc-sign group-tz) (utc-sign (location-timezone (first locations))))
          (equal? (utc-mins group-tz) (utc-mins (location-timezone (first locations)))))
     (cons (location-city (first locations)) (working-group (rest locations) group-tz))]
    [else (working-group (rest locations) group-tz)])) ```

对<代码>一文不值可以使用
map
重写。假设我们有一个函数,它向列表中的每个元素添加3:

(define (add3 lst)
  (if (null? lst)
      '()
      (cons (+ (car lst) 3)
            (add3 (cdr lst)))))
一个列表的映射如下所示:

(define (map f lst)
  (if (null? lst)
      '()
      (cons (f (car lst))
            (map f (cdr lst))))
(define (foldr f init lst)
  (if (null? lst)
      init
      (f (car lst)
         (foldr f init (cdr lst)))))
查看这些内容,您可以看到带有map的
add3
只需要将重点放在添加3上。基本上,您需要传递一个带有一个参数的函数,该参数将向该参数添加3:

(define (add3-wm lst)
  (map (lambda (v) (+ v 3)) lst))
现在,一个列表的
foldr
如下所示:

(define (map f lst)
  (if (null? lst)
      '()
      (cons (f (car lst))
            (map f (cdr lst))))
(define (foldr f init lst)
  (if (null? lst)
      init
      (f (car lst)
         (foldr f init (cdr lst)))))
在这里您可以看到,
cons
没有完成,因此使用
foldr
重写
add3
需要一个组合器,它需要向第一个参数添加3,并将两个参数组合起来,其中第二个参数是同一过程与后面元素的结果

(define (add3-fr lst)
  (define (combiner v acc)
    (cons (+ v 3) acc))
  (foldr combiner '() lst))
实际上,在这里使用
foldr
有些过分,但是如果您有时需要跳过像
workinggroup
这样的元素,这将是一件有趣的事情。在这种情况下,组合器只返回第二个参数。您可以使用
foldr
进行
filter

(define (filter f lst)
  (foldr (lambda (v acc)
           (if (f v)
               (cons v acc)
               acc))
         '()
         lst))
祝你好运