diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-28 00:30:49 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 12ef23e6da7b7186bec20d426c087d757260659d (patch) | |
tree | 8199c8aab45a8694258f9a0378057f761ea0c269 /Scala | |
parent | 765ddc9e51305d6ba703666f42e67ae3fcc91058 (diff) | |
download | coursera-12ef23e6da7b7186bec20d426c087d757260659d.zip coursera-12ef23e6da7b7186bec20d426c087d757260659d.tar.gz |
Scala : sandbox Scala : add 9-lists
Diffstat (limited to 'Scala')
-rw-r--r-- | Scala/sandbox/0-main.scala | 1 | ||||
-rw-r--r-- | Scala/sandbox/9-lists.scala | 19 |
2 files changed, 20 insertions, 0 deletions
diff --git a/Scala/sandbox/0-main.scala b/Scala/sandbox/0-main.scala index b68b57e..2616f8f 100644 --- a/Scala/sandbox/0-main.scala +++ b/Scala/sandbox/0-main.scala @@ -11,5 +11,6 @@ object Main extends App { Natural.run Variance.run PatternMatching.run + Lists.run } diff --git a/Scala/sandbox/9-lists.scala b/Scala/sandbox/9-lists.scala new file mode 100644 index 0000000..7694169 --- /dev/null +++ b/Scala/sandbox/9-lists.scala @@ -0,0 +1,19 @@ +object Lists +{ + + def isort(xs: List[Int]): List[Int] = xs match { + case List() => List() + case y :: ys => insert(y, isort(ys)) + } + + def insert(x: Int, xs: List[Int]): List[Int] = xs match { + case List() => x :: Nil + case y :: ys => if ( x < y ) x :: xs else y :: insert(x, ys) + } + + def run = { + println("Lists") + val l = 3 :: 1 :: 8 :: 4 :: 9 :: Nil + println(isort(l).toString) + } +} |