類型Prop模型命題句子(或者更簡潔的命題)。命題符號(變量)被編碼爲形式P(n)的項,其中n是整數標識符。 Interpr類型將命題解釋實現爲命題符號ID(無重複)的Scala列表。當且僅當n出現在i中時,命題符號P(n)在解釋中才是真實的。斯卡拉的命題邏輯
編寫一個Scala方法,意思是(i:Interpr,p:Prop):布爾,接受解釋i和命題p,並返回斯卡拉布爾值,如果解釋根據命題邏輯的語義滿足命題,否則返回false值。
我很困惑,我可以如何使用模式匹配和遞歸來實現這一點。任何指導將不勝感激!
// Example case
meaning(List(1,2), And(P(1), P(2))) is true
// Example case
meaning(List(1), And(P(1), P(2))) is false
type PVar = Int
type Interpr = List[PVar]
sealed abstract class Prop
case object True extends Prop
case object False extends Prop
case class P(id: PVar) extends Prop // propositional variable
case class Not(sub: Prop) extends Prop
case class Or(sub1: Prop, sub2: Prop) extends Prop // inclusive or
case class Xor(sub1: Prop, sub2: Prop) extends Prop // exclusive or
case class And(sub1: Prop, sub2: Prop) extends Prop
case class Impl(sub1: Prop, sub2: Prop) extends Prop // simple implication =>
case class Iff(sub1: Prop, sub2: Prop) extends Prop // double implication <=>
sealed abstract class Answer
case object Yes extends Answer
case object No extends Answer
def meaning(i: Interpr, p: Prop): Boolean = p match {
// solution here
}
「//解決方案」堆棧溢出,新的做你的作業服務 – pedrofurla
來吧,告訴我們你已經嘗試了什麼或者你在想什麼解決方案。這是一項任務,你應該從中學習。提交其他人的解決方案有什麼意義? –