Skip to content

Instantly share code, notes, and snippets.

@wkimeria
Last active December 4, 2016 01:39
Show Gist options
  • Save wkimeria/762d358be8464889be29d50ea2e9b7e1 to your computer and use it in GitHub Desktop.
Save wkimeria/762d358be8464889be29d50ea2e9b7e1 to your computer and use it in GitHub Desktop.
def reverse[A](as: List[A]): List[A] = as match {
case Nil => Nil
case Cons(x, xs) => foldLeft(xs, Cons(x,Nil))((a,b) => Cons(b, a))
}
def foldRightViaFoldLeft[A,B](as: List[A], z: B)(f: (B, A) => B): B =
foldLeft(reverse(as), z)((b,a) => f(b,a))
def map[A,B](as: List[A])(f: A => B): List[B] =
foldRightViaFoldLeft(as, Nil:List[B])((t,h) => Cons(f(h),t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment