RSS

================================================================================

Before Today

================================================================================

If I was crying
In the van, with my friend
It was for freedom
From myself and from the land

================================================================================

05/08/2010 22:47:16

Scala | パターンマッチ - 特定の文字列とそのひとつ前の要素を削除

def ignore(in: List[String]): List[String] = in match {
  case Nil => Nil
  case _ :: "ignore" :: rest => ignore(rest)
  case x :: rest => x :: ignore(rest)
}

println(ignore(List("hoge", "foo", "ignore", "hello"))) // => List(hoge, hello)

すごいなー。2つすごいと思っていて、一個は単純に再帰のプログラムに慣れてないから、それですごいな、ということと、もうひとつは、こういうパターンマッチって他の言語にあるのかな?(関数型系はありそうだけど)

--------------------------------------------------------------------------------

 

================================================================================

Designed: Robert Boylan
Powered: Tumblr