Scheme 重写列表中的项目

Scheme 重写列表中的项目,scheme,racket,Scheme,Racket,这似乎很简单,但我似乎找不到解决办法。我想用某物替换列表列表中的某个项目,但如果该项目出现多次,则可以随机替换其中一个项目,但不能同时替换两个项目。我想在ISL+中执行此操作 我创建了函数flant,它附加了所有子列表: (check-expect (flatten '((a b) (c) (d e f g) (h i j))) (list 'a 'b 'c 'd 'e 'f 'g 'h 'i 'j)) (define (flatten lol) (foldr a

这似乎很简单,但我似乎找不到解决办法。我想用某物替换列表列表中的某个项目,但如果该项目出现多次,则可以随机替换其中一个项目,但不能同时替换两个项目。我想在ISL+中执行此操作

我创建了函数flant,它附加了所有子列表:

(check-expect (flatten '((a b) (c) (d e f g) (h i j)))
              (list 'a 'b 'c 'd 'e 'f 'g 'h 'i 'j))
(define (flatten lol)
  (foldr append empty lol))
我还进行了重写,它用您选择的任何值替换索引n处的值

(check-expect (rewrite '(x x x - x x x x) 3 'x)
              (list 'x 'x 'x 'x 'x 'x 'x 'x))
(define (rewrite ls n val)
  (cond 
    [(empty? ls) (error "error")]
    [(= n 0) (cons val (rest ls))]
    [else (cons (first ls) (rewrite (rest ls) (sub1 n) val))]))
问题是,我不知道如何将此应用于列表列表,而且我也不知道如果其中一项出现多次,如何随机替换它。这是我为最终产品准备的,但这可能不是我要走的路:

(define (fullreplace b)
  (local [ 
;makes a list of nested lists of each index the element occurs
;problem is that it makes a list of nested lists so I can't use flatten either
(define (position ls ele n)
  (cond [(empty? ls) 0]
        [(equal? ele (first ls)) (list n (position (rest ls) ele (add1 n))) ]
        [else (position (rest ls) ele (+ 1 n))]))]
;lol-full? checks if the item occurs in the list of lists at all
    (if (lol-full? b) b  (rewrite (flatten b) 
                                       (position (flatten b) '- 0)
                                       "item replaced"))))
;just used for testing 
(define lol2 (list
            (list 2 2 2 2)
            (list 4 '- 4 '-)
            (list '- 8 8 8)
            (list 16 '- '- 16)))
(2)可将本文件或任何其他文件退回:

(list
 (list 2 2 2 2)
 (list 4 '- 4 2)
 (list '- 8 8 8)
 (list 16 '- '- 16))

我在这方面已经做了一段时间了,所以任何新的见解都会大有帮助。谢谢

如何用另一个值替换所有出现的值:

(定义(替换所有新值haystack)
(cond((等针草垛)新值)
((一对?干草堆)
(缺点(替换所有新值(汽车干草堆))
(更换所有指针新值(cdr haystack)))
(或干草堆)
唯一需要更改的是检查第一部分是否构成更改。如果是的话,你就不换另一半。使用
equal?
比较结构

这不是随机的。它将通过在
cdr
之前执行
car
cdr
car
之前执行
car

来替换它发现的第一个事件,“随机”部分是导致此问题病理化的原因。如果你能替换第一次出现的,那就很容易了。但要替换随机事件,您必须首先知道有多少个事件。因此,在你更换物品之前,你必须先进行一次盘点:

(define (count/recursive val tree)
  (cond ((equal? val tree)
         1)
        (else (foldl (λ (next-value total)
                       (cond ((equal? val next-value)
                              (add1 total))
                             ((list? next-value)
                              (+ total (count/recursive val next-value)))
                             (else total))) 0 tree))))
然后,您需要一个可以替换第n次出现的值的函数:

(define (replace/recursive val replace-with n tree)
  (cond ((equal? val tree)
         replace-with)
        (else
         (cdr
           (foldl (λ (next-value total/output-tree)
                    (local ((define total (car total/output-tree))
                            (define output-tree (cdr total/output-tree)))
                      (cond ((equal? next-value val)                                  
                             (cons (add1 total)
                                   (cons (if (= total n) replace-with next-value) output-tree)))
                            ((list? next-value)
                             (cons (+ total (count/recursive val next-value))
                                   (cons (replace/recursive val replace-with (- n total) next-value) 
                                         output-tree)))
                            (else (cons total (cons next-value output-tree)))))) (cons 0 empty) tree)))))
最后,使用
random
选择要替换的实例,使用
count/recursive
限制一个数
random
选择的高度:

(define original '((x x (x y x) a b (((c x z x) x) y x x))))
(replace/recursive 'x '- (random (count/recursive 'x original)) original)

什么是针?你能举个例子吗。还有双人床吗?在ISL+中似乎没有定义“针”和“干草堆”是搜索算法中使用的名称。“针”是要找到的东西。“干草堆”是值得一看的东西。有关更多信息,请参阅。对于
pair?
(不是
pairs?
),这在ISL中称为
cons?
。(
pair?
是标准方案名称,而不是
cons?
)递归遍历列表,并在每次找到匹配项时递增累加器。在间隔
[0..累加器)
。第二次遍历列表,每次匹配时增加一个新的累加器。当累加器与随机数匹配时,替换下一个匹配项。我不知道该怎么做。我该如何为此制作累加器?它工作了!我正要睡觉,但我明天肯定会查看它。非常感谢中国!