Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created June 2, 2009 04:07
Show Gist options
  • Save gclaramunt/122003 to your computer and use it in GitHub Desktop.
Save gclaramunt/122003 to your computer and use it in GitHub Desktop.
Math and Logic symbols for Scala
package scrapbook
object PlayWithUTF {
val Π = Math.Pi
def √(x:Double)=Math.sqrt(x)
val x= √(9*Π)
def ∑(r:Range)(f:Int =>Int)=r.reduceLeft(_+ f(_))
def ∏(r:Range)(f:Int =>Int)=r.reduceLeft(_* f(_))
val s= ∑(1 to 100)(x=>x^2)
val p= ∑(1 to 100 by 2)(x=>x^2)
class LogicBoolean(val value:Boolean) {
override def toString=value.toString
def V(b:LogicBoolean)=new LogicBoolean(this.value || b.value)
def Λ(b:LogicBoolean)=new LogicBoolean(this.value && b.value)
def →(b:LogicBoolean)=(¬(this)) V (this Λ b)
}
def ¬(a:LogicBoolean)=new LogicBoolean(!a.value)
implicit def boolToLogicBool(x:Boolean)=new LogicBoolean(x)
def main(args : Array[String]) : Unit = {
val A=true
val B=false
println("¬A :"+ ¬(A))
println("¬B :"+ ¬(B))
println("A Λ B :"+ (A Λ B))
println("A V B :"+ (A V B))
println("A → B :" + (A → B))
println("B → A :" + (B → A))
val r=List(true,false)
def expr(a:Boolean,b:Boolean,c:Boolean,d:Boolean)=
( (a Λ b) V ¬(a Λ c) ) Λ ((c V d) Λ ¬(b) )
//let's print the truth table
println ("\na\tb\tc\td\t(a Λ b) V ¬(a Λ c) ) Λ ((c V d) Λ ¬(b)")
for (
a <- r;
b <- r;
c <- r;
d <- r
) println (a+"\t"+b+"\t"+c+"\t"+d+"\t"+expr(a,b,c,d))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment