如何替换列表中特定位置的元素? [英] How to replace an element in a specific position of a list?

查看:639
本文介绍了如何替换列表中特定位置的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换列表中特定位置的元素。我有这个到目前为止:

 (define alon(list 1 2 3 4 5 6 7 8 9 10))
(定义pos(list 3 6 9));; (定义插入(list-insert xalon xpos xins counter)$ b(定义插入(listabc));;替换

$ b(cond
((empty xins)(cons(rest xalon)'()))
((= counter(first pos))
(cons(first xins) -insert(rest xalon)(rest xpos)(rest xins)
(add1 counter))))
(else
(cons(first xalon)(list-insert(rest xalon)xpos xins(add1 counter))))))

当我做 -insert alon pos插入1)我得到一个错误 first:期望一个非空列表;给出:'()我认为我的问题是当(= counter(first pos))是 true ,我再次调用 list-insert 它不会执行(rest xpos),所以我最终与相同的职位列表,但计数器增量,我结束了空列表,从而错误。

我认为除了像我说的(rest xpos)计数器(第一个pos)) true 并且我调用 list-insert p>

我做错了什么,我该如何解决这个问题,是否有更简单的方法在lambda中学生级别上实现这一点?



感谢您的帮助。

解决方案

替换(= counter (code $)(= counter(first xpos)

  code(define(list-insert xalon xpos xins counter)
(cond
((empty?xins)(rest xalon));不需要使用'()
((空xalon)null);检查空xalon也是有意义的
((= counter(first xpos)); xpos,not pos
(cons(first xins)
list-insert(rest xalon)
(rest xpos)
(rest xins)
(add1 counter))))
(else
(cons(first xalon)
(list-insert(rest xalon)
xpos
xins
add1 counter))))))


I want to replace an element in a specific position of a list. I have this so far:

(define alon (list 1 2 3 4 5 6 7 8 9 10))
(define pos (list 3 6 9)) ;; list with positions to be replaced
(define insert (list "a" "b" "c")) ;;replacement

(define (list-insert xalon xpos xins counter)
  (cond
    ((empty? xins) (cons (rest xalon) '()))
    ((= counter (first pos)) 
     (cons (first xins) (list-insert (rest xalon) (rest xpos) (rest xins) 
                                     (add1 counter))))
  (else
   (cons (first xalon) (list-insert (rest xalon) xpos xins (add1 counter))))))

When I do (list-insert alon pos inserted 1) I get an error first: expects a non-empty list; given: '() I think my problem is when (= counter (first pos)) is true and I call list-insert again it doesn't do (rest xpos) so I end up with the same list of positions but the counter increments and I end up with the empty list thus the error.

I think everything works except of like I said (rest xpos) when (= counter (first pos)) is true and I call list-insert.

What exactly do I do wrong, how do I fix this and is there an easier way to implement this on the intermidiate student level with lambda?

Thank you for your help.

解决方案

Replace (= counter (first pos) with (= counter (first xpos):

(define (list-insert xalon xpos xins counter)
  (cond
    ((empty? xins) (rest xalon)) ; no need to cons with '()
    ((empty? xalon) null)        ; makes sense to check for empty xalon also
    ((= counter (first xpos))    ; xpos, not pos
     (cons (first xins)
           (list-insert (rest xalon)
                        (rest xpos)
                        (rest xins) 
                        (add1 counter))))
    (else
     (cons (first xalon)
           (list-insert (rest xalon)
                        xpos
                        xins
                        (add1 counter))))))

这篇关于如何替换列表中特定位置的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆