Skip to content
Browse files

new stuff

  • Loading branch information...
1 parent 8e14b39 commit 968e3d7f378f66b451866c70dd2bd2e1ff7562c4 @derekwyatt committed Oct 26, 2010
Showing with 40 additions and 0 deletions.
  1. +18 −0 beginning-scala/MList.scala
  2. +19 −0 beginning-scala/Patterns.scala
  3. +3 −0 beginning-scala/build.sh
View
18 beginning-scala/MList.scala
@@ -0,0 +1,18 @@
+
+// This file needs to be compiled and then run
+
+class MList[+T] {
+ def ?:[B >: T](x: B): MList[B] = new ?:(x, this)
+}
+
+case object MNil extends MList[Nothing]
+
+case class ?:[T](hd: T, tail: MList[T]) extends MList[T]
+
+def tryMList(in: MList[Any]) = in match {
+ case 1 ?: MNil => "foo"
+ case 1 ?: _ => "bar"
+ case _ => "baz"
+}
+
+println(tryMList(1 ?: 2 ?: MNil))
View
19 beginning-scala/Patterns.scala
@@ -0,0 +1,19 @@
+
+def sumIf(in: List[Int], cmp: Int => Boolean): Int = in match {
+ case Nil => 0
+ case x :: rest if cmp(x) => x + sumIf(rest, cmp)
+ case _ :: rest => sumIf(rest, cmp)
+}
+
+val l = (1 to 200).toList
+println(sumIf(l, { n => n % 2 == 1 }).toString)
+
+def noPairs[T](in: List[T]): List[T] = in match {
+ case Nil => Nil
+ case a :: b :: rest if a == b => noPairs(a :: rest)
+ case a :: rest => a :: noPairs(rest)
+}
+
+val m = ((1 to 10).toList ::: (1 to 10).toList).sorted
+println(m)
+println(noPairs(m))
View
3 beginning-scala/build.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+scalac -d out -deprecation $*

0 comments on commit 968e3d7

Please sign in to comment.
Something went wrong with that request. Please try again.