Skip to content

Instantly share code, notes, and snippets.

@copumpkin
Last active December 12, 2015 10:29
Show Gist options
  • Save copumpkin/4759099 to your computer and use it in GitHub Desktop.
Save copumpkin/4759099 to your computer and use it in GitHub Desktop.
zipWith/foldr challenge!
-- This is the type we're working with.
data List a = List (forall r. (a -> r -> r) -> r -> r)
-- Implement this signature. It should not be inefficient, so if you're planning
-- on writing something like tail (notoriously inefficient using foldr) on our
-- List type, think again! It should be possible to write with the same time
-- complexity as the original. And of course, converting to/from a conventional
-- list is cheating :)
zipWith :: (a -> b -> c) -> List a -> List b -> List c
zipWith = error "YOUR TASK"
// Can haz first-class polymorphism kthx?
abstract class List[A] {
def foldr[R](f: (A, R) => R, z: R): R
}
// This order is nicer for inference...
def zipWith[A,B,C](xs: List[A], ys: List[B])(f: (A, B) => C): List[C] = {
sys.error("YOUR TASK")
}
// Be aware that I have not tried porting the solution I know of to Scala, so
// I don't know if it breaks. It might have some tail call issues, but I'd
// expect it to work other than that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment