【问题标题】:Delete a value from every pair in a list where the first element of the pair is in another list从列表中的每对中删除一个值,其中该对的第一个元素在另一个列表中
【发布时间】:2013-11-14 03:42:50
【问题描述】:

这真是令人困惑....

假设我们有 2 个列表

(define abc  '((1 (1 2 3)) (2 (2 3)) (4 (1 3))))
(define keys '(1 2))

(define value 2)

我想编写一个函数,从具有第二个列表中第一个元素的第一个列表的每对中的第二个元素中删除提供的值

eg. define deleteValue(abc keys value )

and result   '((1 (1  3)) (2 (3)) (4 (1 3))))

我有这段代码可以做其他事情,但也许它很有用

(define (decrement-assoc-values alist keys)
  (map (lambda (ass)
         (if (member (car ass) keys)
             (list (car ass) (- (cadr ass) 1))
             ass))
       alist))

(define abc  '((1  3) (2 3) (4  3)))
(define lst '(1 2))

(decrement-assoc-values abc  lst )
Result : '((1 2) (2 2) (4 3))

这会将第二个列表中具有第一个元素的对的第二个元素减 1

【问题讨论】:

  • 我不确定我是否正确理解了该功能。 deleteValue(abc keys 2) 中的 2 是什么意思?
  • 是我们要从每对的第二部分中删除的值。我在示例中进行了 1-2 次更新。这有点令人困惑。我的错...
  • 该过程与您已有的示例非常相似。只需将(- (cadr ass) 1) 替换为(remove* value (cadr ass))。有关更多详细信息,请参阅我的答案

标签: list scheme element


【解决方案1】:

应该这样做。

(define (delete-value abc keys value)
  (map (λ (pair)
         (if(member (car pair) keys)
            (list (car pair)
                  (filter (λ(x)
                             (not(equal? x value))) 
                          (car (cdr pair))))
         pair)) 
    abc))

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 2016-02-26
    • 2022-11-13
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2022-09-23
    相关资源
    最近更新 更多