【问题标题】:Inserting an element at given position in list在列表中的给定位置插入元素
【发布时间】:2013-03-11 19:49:23
【问题描述】:

我不知道如何在列表中的 n 位置附加一个元素。 例如:

(insert-at new k lis)
(insert-at ’N 2 ’(a b c d e f))
=>
’(a b N c d e f)

可以吗?:

(define (insert-at new k lis)
  (cond (( null? lis)
         (list new))   
        (zero? k   
         (cons new lis))   
        (else       
         (cons (car lis)
               (insert-at new (- k 1) (cdr lis))))))

【问题讨论】:

  • 是的,在 zero? k 条件周围加上括号后,就像你在 null? lis 周围一样。如果(zero? k) 不为零,则可能会在(null? lis) 下添加对(zero? k) 的检查以不同方式处理它(即报告错误或其他内容)。

标签: list scheme cons


【解决方案1】:

我会给你一些提示 - 因为这看起来像家庭作业,我不能给你一个直接的答案,如果你通过自己的方式找到解决方案会更有用。填空:

(define (insert-at new k lis)
  (cond (<???>       ; if the list is empty
         <???>)      ; return a list with the single element `new`
        (<???>       ; if `k` is zero
         <???>)      ; cons `new` with the list
        (else        ; otherwise
         (cons <???> ; cons the lists' current element and
               (insert-at new <???> <???>))))) ; advance the recursion

请注意,这里的“推进递归”意味着传递列表的其余部分并将k 索引递减一个单位。一旦k 索引为零或到达列表的末尾,我们就完成了。不要忘记测试程序:

(insert-at 'N 2 '(a b c d e f))
=> '(a b N c d e f)

(insert-at 'N 0 '(a b c))
=> '(N a b c)

(insert-at 'N 3 '(a b c))
=> '(a b c N)

【讨论】:

  • 感谢各位帮助,我是这种编程语言的菜鸟:(define (insert-at new k lis) (cond (if (null? lis) '(new))) (零? k (cons new lis)) (else (cons (car l) (insert-at new (cdr l) (- n 1))))))))
  • 你几乎明白了!在递归调用中,您以错误的顺序发送参数,请更正这一点。删除 if,这是不对的 - 只需使用 cond 作为条件,不需要 if。最后,返回单元素列表的正确方法是:(list new),这不起作用:'(new)
  • > (define (insert-at new k lis) (cond (null? (list new)) (zero? k (cons new lis)) (else (cons (car l) (insert- at new (- n 1) (cdr l))))) > (插入-at 4 3 '(1 2 3 5 6 7 8)) (4) >
  • @NUTIC 很多括号问题。请先了解如何使用cond 指令的look,或者使用一系列嵌套的ifs。用我回答中的例子测试一下
【解决方案2】:

如果你有两个功能:

  1. take-n - 将前 N 个元素作为列表返回,并且
  2. last-n - 返回最后 N 个元素的列表

那么你可以写:

(define (insert-at value index list)
  (let ((len (length list)))
    (assert (<= 0 index len))
    (append (take-n index list)
            (list value)
            (last-n (- len index) list))))

【讨论】:

    【解决方案3】:

    (DEFUN INS-ELEM (第N项列表)

    (条件

    ((

    ((= Nth 1) (CONS 项目列表))

    ((ENDP列表)(错误“索引太大”))

    (T (CONS (FIRST list) (INS-ELEM (1- Nth) item(REST list))))))

    然后,只需调用 (INS-ELEM 2 'A '(B C D E F)) 并自己查看结果。

    祝你好运!

    【讨论】:

      【解决方案4】:

      #lang 球拍

      (define (insert-at Index item lst)
      
      (cond
      
      [(< Index 1) (error "Index too small: " Index)]
      
      [(= Index 1) (cons item lst)]
      
      [(> Index (length lst)) (error "Index too big: " Index)]
      
      [else (cons (first lst) (insert-at (- Index 1) item (rest lst)))]))
      

      user3660248

      您关于如何解决此问题的想法似乎正确且有意义,但您在 Racket 中的实现不正确,我已修复它,现在它可以工作了 :)

      【讨论】:

      • 反复调用length 是一种反模式。在列表处理函数中调用 length 通常是一种代码味道(这里也是如此)。
      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多