球拍-如何更改结构的属性值

时间:2019-11-28 16:33:31

标签: racket

我具有以下结构:

(define-struct my-struct (label value))

我想更改列表中所有项目的属性值。我想在所有项目中将值设置为2。

(define (change-value mylist priority)
  ( cond
    [( empty? mylist) mylist] 
    [else ( cons ((struct-copy my-struct (first mylist) [value 2]) ) (change-value (rest mylist) value) )]))
)

我正在尝试使用struct-copy,但出现以下错误:

struct-copy: this function is not defined

知道为什么我会收到此错误吗?我应该导入任何库吗?

1 个答案:

答案 0 :(得分:0)

尚不清楚您打算如何执行代码。这是一个遍历“ my-struct”列表并将所有“ value”属性设置为2的版本:

#lang racket

(define-struct my-struct (label value) #:transparent)

(define (change-value mylist)
  (for/fold ([result '()])
            ([s (in-list mylist)])
    (cons (struct-copy my-struct s [value 2]) result)))

如果我运行它,我将得到:

> (define l (list (my-struct 1 3) (my-struct 4 6) (my-struct 8 7)))
> l
(list (my-struct 1 3) (my-struct 4 6) (my-struct 8 7))
>
> (change-value l)
(list (my-struct 8 2) (my-struct 4 2) (my-struct 1 2))