方案:从嵌套列表中删除元素

发布于 2024-10-17 09:18:59 字数 247 浏览 1 评论 0原文

给定一个命题公式,即((a和(b暗示c)或(d和(e暗示f)))), 我需要编写一个Scheme函数来删除连接词andimpliesor。 函数的返回包含公式中的所有变量。例如, <代码>(abcdef)。

我什至不知道如何开始这样做,因为我不确定如何进入嵌套列表并删除和cons某些变量和连接词。

Given a propositional formula, i. e. ((a and (b implies c) or (d and (e implies f)))),
I need to write a Scheme function to remove the connectives, and, implies, or.
The the return of the function contains all variables in the formula. For instance,
(a b c d e f).

I am not sure how to even get started on this, because I am unsure how to get inside the nested lists and remove and cons certain variables and connectives.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-10-24 09:18:59

我会从以下内容开始:

(define somelist
  (list 'a 'and (list 'b 'implies 'c) 
        'or (list 'd 'and (list 'e 'implies 'f))))

(define (remove-vars xs ys)
  (let ((xs-f (flatten xs)))
    (filter-two xs-f ys)))

(define (filter-two xs ys)
  (foldr (lambda(y acc)
           (filter (lambda(x) (not (eq? x y))) acc))
         xs
         ys))

测试:

> (remove-vars somelist (list 'and 'or 'implies))
(a b c d e f)
> (remove-vars somelist (list 'and 'or))
(a b implies c d e implies f)

更新:好的,@karategeek6 报告说,他没有展平过滤器在他的Scheme解释器中,我不确定你这样做,所以让我们手动实现它们,因为R^6RS中也没有filterflatten

(define (my-flatten xs)
  (foldr
   (lambda(x acc)
     (if (list? x)
         (append (my-flatten x) acc)
         (cons x acc)))
   (list)
   xs))

(define (my-filter pred xs)
  (let recur ((xs xs)
              (acc (list)))
    (if (empty? xs)
        (reverse acc)
        (if (pred (car xs))
            (recur (cdr xs) (cons (car xs) acc))
            (recur (cdr xs) acc)))))

修改remove-vars 和 filter-two

(define (remove-vars xs ys)
  (let ((xs-f (my-flatten xs)))
    (filter-two xs-f ys)))

(define (filter-two xs ys)
  (foldr (lambda(y acc)
           (my-filter (lambda(x) (not (eq? x y))) acc))
         xs
         ys))

您应该获得与上述过程的早期版本相同的输出。

I would start up with something like the following:

(define somelist
  (list 'a 'and (list 'b 'implies 'c) 
        'or (list 'd 'and (list 'e 'implies 'f))))

(define (remove-vars xs ys)
  (let ((xs-f (flatten xs)))
    (filter-two xs-f ys)))

(define (filter-two xs ys)
  (foldr (lambda(y acc)
           (filter (lambda(x) (not (eq? x y))) acc))
         xs
         ys))

Test:

> (remove-vars somelist (list 'and 'or 'implies))
(a b c d e f)
> (remove-vars somelist (list 'and 'or))
(a b implies c d e implies f)

UPDATE: OK, @karategeek6 reported, that he doesn't have flatten and filter in his Scheme interpreter, and I'm not sure you do, so let's implement them manually, because there are no filter and flatten in R^6RS either:

(define (my-flatten xs)
  (foldr
   (lambda(x acc)
     (if (list? x)
         (append (my-flatten x) acc)
         (cons x acc)))
   (list)
   xs))

(define (my-filter pred xs)
  (let recur ((xs xs)
              (acc (list)))
    (if (empty? xs)
        (reverse acc)
        (if (pred (car xs))
            (recur (cdr xs) (cons (car xs) acc))
            (recur (cdr xs) acc)))))

Modify remove-vars and filter-two appropriately:

(define (remove-vars xs ys)
  (let ((xs-f (my-flatten xs)))
    (filter-two xs-f ys)))

(define (filter-two xs ys)
  (foldr (lambda(y acc)
           (my-filter (lambda(x) (not (eq? x y))) acc))
         xs
         ys))

You should get the same output as with previous versions of the above procedures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文