【logic】归结原理
drracket吧
全部回复
仅看楼主
level 3
这两天在写一个检查归结的racket程序
(define (complementary-literals? l1 l2)
(let* ([l1str (symbol->string l1)]
[l2str (symbol->string l2)]
[l1neg (equal? #\~ (string-ref l1str 0))]
[l2neg (equal? #\~ (string-ref l2str 0))]
[symbol1 (if l1neg (substring l1str 1) l1str)]
[symbol2 (if l2neg (substring l2str 1) l2str)])
(and (xor l1neg l2neg)
(equal? symbol1 symbol2))))
(define (resolve clause1 clause2);;clause1 and 2 both list '(a v c) '(~a b d)
(let ([temp #f])
(for* ([next1 clause1]
[next2 clause2]);;next1 and 2 both symbol from list1 and 2
(cond
[(complementary-literals? next1 next2) (set! temp (delete next2 (delete next1 (append clause1 clause2) '()) '()))]))
(if (not (boolean? temp)) temp -1)));;return -1 if two clauses different, return list if two cluases can combine
(define (delete n lst clean);;delete an element from a list
(cond
((empty? lst) clean)
((equal? n (car lst)) (delete n (cdr lst) clean))
(else
(delete n (cdr lst) (append clean (list (car lst)))))))
这里面resolve作用是拿2个list 对比2个list里每一个symbol是否有例如a ~a这种形式
如果有就返回剩下的比如(resolve '(a b c) '(~a d e))就会返回‘(b c d e)
但是如果是(resolve '(a b c) '(~a ~b d)) 我期望的是返回‘(c d)
现在返回给我的是’(a c ~a d)
我现在想的是做一个recursion 可是停止的情况想不出来
2014年03月13日 10点03分 1
level 11
(define (complementary-literals? l1 l2)
(let* ([l1str (symbol->string l1)]
[l2str (symbol->string l2)]
[l1neg (equal? #\~ (string-ref l1str 0))]
[l2neg (equal? #\~ (string-ref l2str 0))]
[symbol1 (if l1neg (substring l1str 1) l1str)]
[symbol2 (if l2neg (substring l2str 1) l2str)])
(and (xor l1neg l2neg)
(equal? symbol1 symbol2))))
(define c-l? complementary-literals?)
(define (resolve l1 l2)
(cond ((null? l1) l2)
((null? l2) l1)
((c-l? (car l1) (car l2))
(resolve (cdr l1) (cdr l2)))
(else
(append l1 l2))))
;;;test
(resolve '(a b c) '(a d e))
(resolve '(a b c) '(~a d e))
(resolve '(a b c) '(~a ~b d))
2014年03月13日 10点03分 2
运行结果: '(a b c a d e) '(b c d e) '(c d) 不知道楼主是不是这个意思[疑问]
2014年03月13日 10点03分
list 里面是没有order的 有可能是'(b c a) '(~a d f) 这种样子
2014年03月13日 11点03分
level 11
#lang racket
(define (rm-elem? elem)
(let* ((elem-str (symbol->string elem)))
(cond ((equal? #\~ (string-ref elem-str 0))
(string->symbol (substring elem-str 1)))
(else #f))))
(define (resolve-h l1 rm-l temp)
(cond ((or (null? l1) (null? rm-l)) (list rm-l temp))
((rm-elem? (car l1))
=> (lambda (result)
(resolve-h (cdr l1) (remove result rm-l) temp)))
(else
(resolve-h (cdr l1) rm-l (cons (car l1) temp)))))
(define (resolve l1 l2)
(let* ((result-lst ((lambda (x)
(resolve-h (car x) (cadr x) '()))
(resolve-h l1 l2 '())))
(fl1 (car result-lst))
(fl2 (cadr result-lst)))
(remove-duplicates
(append fl1 fl2))))
;;;test
(resolve '(a b c) '(a d e))
(resolve '(a b c) '(~a d e))
(resolve '(a b c) '(~a ~b d))
(resolve '(b c a) '(~a d f))
(resolve '(b d a c f) '(a c ~f e ~b d))
#|
result:
'(c b a e d)
'(c b e d)
'(c d)
'(c b f d)
'(c a d e)
|#
2014年03月13日 13点03分 3
看看是不是这样? 楼主学的什么学科呀?逻辑学? 能发参考书吗,我也想学习学习呢~~~[乖]
2014年03月13日 13点03分
(resolve '(~a b t c ~r d) '(a d r b ~t o p)) ----- '(d c b p o)
2014年03月13日 13点03分
修改 (list rm-l temp) 改成 (list rm-l (reverse temp) || 能够让归结后,字母顺序不变。
2014年03月13日 14点03分
'(a b c d e)'(b c d e)'(c d)'(b c d f)'(d a c e)'(b c d o p)
2014年03月13日 14点03分
1