Skip to content

Instantly share code, notes, and snippets.

@gustavofranke
Created March 20, 2017 10:52
Show Gist options
  • Save gustavofranke/68b438cfa0d06be1449c290a36c34840 to your computer and use it in GitHub Desktop.
Save gustavofranke/68b438cfa0d06be1449c290a36c34840 to your computer and use it in GitHub Desktop.
def rev[A](str: List[A]): List[A] = str match {
case Nil => Nil
case x :: Nil => x :: Nil
case x :: y :: Nil => y :: x :: Nil
case x :: xs => xs.last +: rev(xs.init) :+ x
}
val gus = "Gustavo".toList //gus: List[Char] = List(G, u, s, t, a, v, o)
rev(gus) //res0: List[Char] = List(o, v, a, t, s, u, G)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment