Last active
December 4, 2016 01:39
-
-
Save wkimeria/762d358be8464889be29d50ea2e9b7e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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