added midterm
This commit is contained in:
		
							
								
								
									
										7
									
								
								src/MidTerm1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/MidTerm1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| val pi: List[Long] = List(3, 1, 4, 1, 5, 9, 2) | ||||
| val slice: List[Int] = List(4, 1, 5) | ||||
|  | ||||
| pi.indexOfSlice(slice) | ||||
|  | ||||
| //List("Hello", "world", "Scala").foldRight(0)((a, b) => a + b.length) | ||||
| List("Hello", "world", "Scala").foldLeft(0)((a, b) => a + b.length) | ||||
							
								
								
									
										34
									
								
								src/MidTerm1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/MidTerm1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
| @tailrec | ||||
| def foldLeft[A, B](list: List[A])(init: B)(f: (B, A) => B): B = { | ||||
|   list match { | ||||
|     case Nil => init | ||||
|     case head::tail => foldLeft(tail)(f(init, head))(f) | ||||
|   } | ||||
| } | ||||
|  | ||||
| @tailrec | ||||
| def dropWhile[T](list: List[T])(predicate: T => Boolean): List[T] = { | ||||
|   list match { | ||||
|     case head::tail if predicate(head) => dropWhile(tail)(predicate) | ||||
|     case _ => list | ||||
|   } | ||||
| } | ||||
|  | ||||
| def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = { | ||||
|   list.foldRight(List.empty[T])((e: T, l: List[T]) => { | ||||
|     //if (preds.foldLeft(true)((a, b) => a && b(e))) e::l | ||||
|     if (preds.forall(b => b(e))) e::l | ||||
|     else l | ||||
|   }) | ||||
| } | ||||
|  | ||||
| def fixedPoint(f: Int => Int): Int => Int = { | ||||
|   def func(x: Int): Int = { | ||||
|     val y: Int = f(x) | ||||
|     if (x == y) y | ||||
|     else f(y) | ||||
|   } | ||||
|   func | ||||
| } | ||||
							
								
								
									
										37
									
								
								src/MidTerm1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/MidTerm1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
| abstract class Text { | ||||
|   def isEmpty: Boolean = { | ||||
|     this match { | ||||
|       case Chars(cs) => cs.isEmpty | ||||
|       case Concat(t1, t2) => t1.isEmpty && t2.isEmpty | ||||
|     } | ||||
|   } | ||||
|   def head: Char = { | ||||
|     this match { | ||||
|       case Chars(cs) => cs.head | ||||
|       case Concat(t1, t2) => if (t1.isEmpty) t2.head else t1.head | ||||
|     } | ||||
|   } | ||||
|   def tail: Text = { | ||||
|     this match { | ||||
|       case Chars(cs) => Chars(cs.tail) | ||||
|       case Concat(t1, t2) => if (t2.isEmpty) t1.tail else Concat(t1, t2.tail) | ||||
|     } | ||||
|   } | ||||
|   def map(f: Char => Char): Text = { | ||||
|     this match { | ||||
|       case Chars(cs) => Chars(cs.map(f)) | ||||
|       case Concat(t1, t2) => Concat(t1.map(f), t2.map(f)) | ||||
|     } | ||||
|   } | ||||
| } | ||||
| case class Chars(cs: List[Char]) extends Text | ||||
| case class Concat(t1: Text, t2: Text) extends Text | ||||
|  | ||||
| @tailrec | ||||
| def equals(t1: Text, t2: Text): Boolean = { | ||||
|   if (t1.isEmpty) t2.isEmpty | ||||
|   else if (t2.isEmpty) false | ||||
|   else (t1.head == t2.head) && equals(t1.tail, t2.tail) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user