Skip to content

Instantly share code, notes, and snippets.

@cokeSchlumpf
Created July 16, 2013 21:43
Show Gist options
  • Save cokeSchlumpf/6015425 to your computer and use it in GitHub Desktop.
Save cokeSchlumpf/6015425 to your computer and use it in GitHub Desktop.
Logische Algebra in Scala ...
package com.ibm.fp.scala
object PatternMatching extends App {
sealed trait Expression
sealed trait UnaryExpression extends Expression { val unary: Expression }
sealed trait BinaryExpression extends Expression { val left: Expression; val right: Expression }
case class Not(unary: Expression) extends UnaryExpression
case class And(left: Expression, right: Expression) extends BinaryExpression
case class Or(left: Expression, right: Expression) extends BinaryExpression
case object True extends Expression
case object False extends Expression
def eval(e: Expression): Boolean = e match {
case And(l, r) => eval(l) && eval(r)
case Or(l, r) => eval(l) || eval(r)
case Not(u) => !eval(u)
case True => true
case False => false
}
val a = Not(And(True, Not(Or(False, True))))
println("a = " + a)
println("eval(a) = " + eval(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment