-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
- Loading branch information
1 parent
62bc15f
commit 26dbbe5
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
Exercise 2.5.2.10 | ||
Transform a given sequence xs: Seq[Int] into a sequence of type | ||
Seq[(Int, Int)] of pairs that skip one neighbor. | ||
Implement this transformation as a function skip1 with a type | ||
parameter A instead of the type Int. | ||
The required type signature and a sample test: | ||
def skip1[A](xs: Seq[A]): Seq[(A, A)] = ??? | ||
scala> skip1(List(1, 2, 3, 4, 5)) | ||
res0: List[(Int, Int)] = List((1,3), (2,4), (3,5)) | ||
*/ | ||
|
||
def skip1Intern[A](xs: Seq[A], res: Seq[(A, A)] = Seq.empty): Seq[(A, A)] = { | ||
if (xs.length < 3) res.reverse | ||
else | ||
skip1Intern(xs.tail, (xs.head, xs(2)) +: res) | ||
} | ||
|
||
def skip1[A](xs: Seq[A]): Seq[(A, A)] = { | ||
skip1Intern[A](xs: Seq[A]) | ||
} | ||
|
||
val expected: List[(Int, Int)] = List((1,3), (2,4), (3,5)) | ||
val result = skip1(List(1, 2, 3, 4, 5)) | ||
assert(result == expected) | ||
|
||
// scala> :load solution2.5.2.10.scala | ||
// :load solution2.5.2.10.scala | ||
// def skip1Intern[A](xs: Seq[A], res: Seq[(A, A)]): Seq[(A, A)] | ||
// def skip1[A](xs: Seq[A]): Seq[(A, A)] | ||
// val expected: List[(Int, Int)] = List((1,3), (2,4), (3,5)) | ||
// val result: Seq[(Int, Int)] = List((1,3), (2,4), (3,5)) |