生成された真理値表の条件をキャッチするループに小さな問題があります..論理式を入力すると、それが真理値表に変換され、有効か無効か矛盾しているかが解釈されます。これまでのところ、これはそれを解釈するプログラムの一部ですが、無効または有効のみをキャッチしています...これについて教えていただけますか? ありがとう
*edit// これがプログラムの実行方法です:
*******いらっしゃいませ!********
(LogicStart) を入力して開始するか、(終了) を入力していつでも終了します。
;; 読み込まれたファイル MyLogic.lisp
T [2]> (LogicStart) 論理式または式を入力してください: "(p^(~p))"
p (~p) (p^(~p))
T NIL NIL
NIL T NIL
数式が無効です
したがって、入力は論理式のみであり、出力はその式の真理値表です....そしてそれを解釈することもできますが、上記の例は一貫性がない/満足できない (式/式のすべての解釈が偽であるため)
編集を終了
(defun interpret() ; interpret if valid or not or inconsistent
(setq lastcolumn (- (column) 1))
(setq lastcolumnROW 1)
(loop
(unless (aref (aref tbl lastcolumn) lastcolumnROW) (progn (princ "The formula is Invalid")(return)))
(setq lastcolumnROW (+ lastcolumnROW 1))
(when (= lastcolumnROW (+ 1 (row))) (progn (princ "The formula is a Tautology ") (return)))
)
)
編集 2:///
これは LogicStart 関数です:
(defun LogicStart()
;Function to run program
(princ "Enter Logical Expression or Formula: " )
(setq input (read))
;Get input
(format t "-----------------------------------------------~C" #\linefeed)
;Create two dimension array(table)
(setq tbl (make-array (column)))
(setq index 0)
(loop
(setf (aref tbl index) (make-array (+ (row) 1)))
(setq index (+ 1 index))
(when (= index (column))(return))
)
(setAtoms)
(setFirstValue)
(tblReplaceValue)
(watchTable)
(format t "-----------------------------------------------~C" #\linefeed)
(interpret)
)
setAtoms 関数:
(defun setAtoms()
;Get ALL possible formula
(setq indexOFTBL (make-array (column)))
(setq openP (make-array (- (column) (length Latoms))))
; Get index of open Parenthesis
(setq cOpenP 0)
(setq closeP (make-array (- (column) (length Latoms))))
;Get index of close Parenthesis
(setq cCloseP 0)
(setq index 0)
(loop
(when (char-equal (char input index) #\()
(progn
(setf (aref openP cOpenP) index)
(setq cOpenP (+ 1 cOpenP))
)
)
(when (char-equal (char input index) #\))
(progn
(setf (aref closeP cCloseP) index)
(setq cCloseP (+ 1 cCloseP))
)
)
(setq index (+ 1 index))
(when (= index (length input)) (return))
)
;(print openP)
;(print closeP)
(setq index 0)
(loop
(if (< index (length Latoms))
(progn
(setf (aref (aref tbl index) 0) (char Latoms index))
(setf (aref indexOFTBL index) index)
)
(progn
(setq OpIndex cOpenP)
(loop
(setq OpIndex (- OpIndex 1))
(setq CpIndex 0)
(loop
(if (or (> (aref openP OpIndex) (aref closeP CpIndex)) (= -1 (aref closeP CpIndex)))
(progn
(setq CpIndex (+ CpIndex 1))
)
(progn
(setf (aref (aref tbl index) 0) (subseq input (aref openP OpIndex) (+ 1 (aref closeP CpIndex))))
(setf (aref closeP CpIndex) -1)
(return)
)
)
(when (= CpIndex (length closeP))(return))
)
(setq index (+ index 1))
(when (= OpIndex 0) (return))
)
(return)
)
)
(setq index (+ index 1))
(when (= index (column)) (return))
)
)
watchTable と列関数
(defun watchTable()
; View table
(setq ro 0)
(loop
(setq co 0)
(loop
(princ(aref (aref tbl co) ro))(format t "~C" #\tab)
(setq co (+ 1 co))
(when (= co (column))(return))
)
(format t "~C" #\linefeed)
(setq ro (+ 1 ro))
(when (= ro (+ (row) 1))(return))
)
)
(defun column()
; Get the number of columns
(+ (atoms) (symbols))
)
//編集 3 したがって、(OR A (NOT A)) の場合、テーブルには @jkiiski のコードで「not A」がありません
A | NOT A | (OR A (NOT A))
----+----------+--------
NIL | T | T
T | NIL | T
This expression is a Tautology.
参照用の別の例: P は Q を暗示していますが、このコードは次のように暗黙を受け入れます: >
; Logical Connectives:
; ~ negation
; - biconditional
; > conditional
; ^ and
; v or
; Example Input:
; "(~((a^b)>c))"
; "(p>q)"
p q p>q
T T T
T NIL NIL
NIL T T
NIL NIL T
Another example:
Enter an expression: "((p>q)^r)"
T <- True
NIL <- False
--------------------------------------------
p q r (p>q) ((p>q)^r)
T T T T T
T T NIL T NIL
T NIL T NIL NIL
T NIL NIL NIL NIL
NIL T T T T
NIL T NIL T NIL
NIL NIL T T T
NIL NIL NIL T NIL
--------------------------------------------
したがって、(p>q)^r では、真理値表に p、q、r、(p>q)、および最後に (p>q)^r が表示されます。
4つ編集//
(defun generate-value-combinations (variables)
(let ((combinations (list)))
(labels ((generate (variables &optional (acc (list)))
(if (endp variables)
(push (reverse acc) combinations)
(loop for value in '(t nil)
for var-cell = (cons (car variables) value)
do (generate (cdr variables) (cons var-cell acc))))))
(generate variables)
combinations)))
to this one?
(defun generate-value-combinations (variables)
(let ((combinations (list)))
(labels ((generate (variables &optional (acc (list)))
(if (endp variables)
(push (reverse acc) combinations)
(loop for value in '(t nil)
for var-cell = (cons (car variables) value)
do (generate (cdr variables) (cons var-cell acc))))))
(generate variables) nreverse combinations)))