Skip to content
Snippets Groups Projects
Commit b66665a0 authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Scans in library

parent ef8df9ad
No related branches found
No related tags found
No related merge requests found
...@@ -330,6 +330,18 @@ sealed abstract class List[T] { ...@@ -330,6 +330,18 @@ sealed abstract class List[T] {
case Nil() => z case Nil() => z
case Cons(h, t) => f(h, t.foldRight(f)(z)) case Cons(h, t) => f(h, t.foldRight(f)(z))
} }
def scanLeft[R](z: R)(f: (R,T) => R): List[R] = this match {
case Nil() => z :: Nil()
case Cons(h,t) => z :: t.scanLeft(f(z,h))(f)
}
def scanRight[R](f: (T,R) => R)(z: R): List[R] = { this match {
case Nil() => z :: Nil()
case Cons(h, t) =>
val rest@Cons(h1,_) = t.scanRight(f)(z)
f(h, h1) :: rest
}} ensuring { !_.isEmpty }
def flatMap[R](f: T => List[R]): List[R] = def flatMap[R](f: T => List[R]): List[R] =
ListOps.flatten(this map f) ListOps.flatten(this map f)
...@@ -502,4 +514,17 @@ object ListSpecs { ...@@ -502,4 +514,17 @@ object ListSpecs {
// }} && // }} &&
// l.foldLeft(z)(f) == l.reverse.foldRight((x:T,y:R) => f(y,x))(z) // l.foldLeft(z)(f) == l.reverse.foldRight((x:T,y:R) => f(y,x))(z)
//}.holds //}.holds
//
//Can't prove this
//@induct
//def scanVsFoldLeft[A,B](l : List[A], z: B, f: (B,A) => B): Boolean = {
// l.scanLeft(z)(f).last == l.foldLeft(z)(f)
//}.holds
@induct
def scanVsFoldRight[A,B](l: List[A], z: B, f: (A,B) => B): Boolean = {
l.scanRight(f)(z).head == l.foldRight(f)(z)
}.holds
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment