Posts

Showing posts with the label functional-programming

Are Lists just a hack to represent sequences functionally?

Are Lists just a hack to represent sequences functionally? I'm very interested in functional programming as a way to represent abstractions without lying as to what they truly are for convenience. Something feels off to me about lists (in the way they are recursively defined in functional programming). Why do functional programming languages generally have lists defined with an Empty case? Is a collection of things really a sum-type? Or is my conception of a list separate from what this is: Empty type List = Empty | Element head (List tail) I know that pattern matching makes the above safe to do, but to me it seems analogous to making all types Option types by default. Is there a term for the right-hand side of this List definition? List Also is this List something from Mathematics? Are we representing things which are truly sequences as lists for convenience? List By clicking "Post Your Answer", you a...

Can I zip more than two lists together in Scala?

Can I zip more than two lists together in Scala? Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the following doesn't work: scala> l reduceLeft ((a, b) => a zip b) <console>:6: error: type mismatch; found : List[(String, String)] required: List[String] l reduceLeft ((a, b) => a zip b) Any suggestions one how to do this? I think I'm missing a very simple way to do it. Update: I'm looking for a solution that can take a List of N Lists with M elements each and create a List of M TupleNs. Update 2: ...