This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package propositional | |
import scala.annotation.tailrec | |
sealed abstract class Formula { | |
def ∧(q:Formula) = Conjunction(this, q) | |
def ∨(q:Formula) = Disjunction(this,q) | |
def →(q:Formula) = Implication(this,q) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To simplify the implementation, foldr_ is introduced. It is the foldr function with a modification in | |
// combine parameter. Instead of (A, =>B) => B, combine has type (A, B, Stream[A])=>B. | |
// This makes combine has an easy access to the stream being processed. | |
// There might be another way to implement the solution, but this one is the one that comes into my head so far. | |
def foldr_[A, B]( combine: (A, =>B, Stream[A]) => B, base: B)( xs: Stream[A]): B = { | |
if (xs.isEmpty) | |
base | |
else | |
combine(xs.head, foldr_(combine, base)(xs.tail), xs) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyA[A, B](val head: A, val tail: Option[MyA[B, A]]) { | |
override def toString() = head + tail.map( "," + _.toString).getOrElse("") | |
def ::( x: B) = new MyA(x, Some(this)) | |
} | |
object MyA { | |
def apply[A,B](value: A) : MyA[A, B] = new MyA(value, None) | |
} | |
// examples: |