Skip to content

Instantly share code, notes, and snippets.

@nephilim
Created August 24, 2010 16:42
Show Gist options
  • Save nephilim/547862 to your computer and use it in GitHub Desktop.
Save nephilim/547862 to your computer and use it in GitHub Desktop.
// 우선 cons 연산자 비슷한 놈을 만들었습니다.
// 이름은 co-ons(코-온스)라 했습니다. :)
// ex) List(1,1,1,2,3) == 1 ~: List(2,3)
object ~: {
def unapply(list:List[Int]):Option[(Int, List[Int])] = {
list match {
case Nil => None;
case x::xs => Some((x, xs.dropWhile( _ == x )));
}
}
}
// 개미수열 = look-and-say sequence
// 코-온스를 패턴 매칭에 활용하여, 개미 수열을 읽어냅니다.
object AntSeq {
def lookAndSay(list:List[Int]):List[Int] = {
list match {
case Nil => Nil;
case x~:xs => x::(list.size-xs.size)::lookAndSay(xs) //co-ons 참고
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment