给定一个命题公式,即i。即((a and (b implies c) or (d and (e implies f))))
, 我需要写一个计划函数删除连接词,and
,implies
,or
。 函数的返回值包含公式中的所有变量。例如, 。方案:从嵌套列表中删除元素
我不知道该如何开始,因为我不确定如何进入嵌套列表并删除某些变量和连接符。
给定一个命题公式,即i。即((a and (b implies c) or (d and (e implies f))))
, 我需要写一个计划函数删除连接词,and
,implies
,or
。 函数的返回值包含公式中的所有变量。例如, 。方案:从嵌套列表中删除元素
我不知道该如何开始,因为我不确定如何进入嵌套列表并删除某些变量和连接符。
我就开始了类似以下内容:
(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)
UPDATE:OK,@ karategeek6报道,说他没有flatten
和filter
在他Scheme解释,我不知道你做什么,所以让我们手动实现它们,因为在R 1 6RS要么没有filter
和flatten
:
(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))
你应该得到的输出与上面的程序以前的版本相同。
过滤器和扁平从哪里来?他们是R6RS的新手?我目前只熟悉R5RS,并且我知道他们不是那个标准。 – cledoux 2011-02-12 18:37:29
在删除一个,两个或所有的连接词后,您的公式会如何?你能举个例子吗? – 2011-02-12 17:26:32