12

What are the key differences between pattern matching in these two languages? I am not referring to syntax, but capability, implementation details, range of use cases and necessity.

Scala applications (eg. Lift and Play) speak proudly about the languages pattern matching prowess. Clojure, on the other hand has a library, core.match, and built in destructuring, which also seems powerful.

*note: The reason I was inspired to ask this question is because of a blog post I saw in which a programmer, as an experiment, built a lisp interpreter using both Scala and Clojure. He said that the Clojure matches broke after a certain length, but could not explain why, but I am really curious to know. You can find this post here: http://www.janvsmachine.net/2013/09/writing-simple-lisp-interpreter-in-clojure.html

10
  • 3
    @gnat The blog post seems really incidental. It seems to me he doesn't really care about an explanation to the blog per se, just a comparison of how a particular language feature differs across two specific languages. Isn't that objectively answerable?
    – Doval
    Commented Apr 25, 2014 at 16:56
  • 3
    In defense of this question: it's objective, well-written, and clear. Why does it matter that the OP read a blog before posting this question? That doesn't change the quality of the question.
    – user39685
    Commented Apr 25, 2014 at 16:56
  • 1
    This isn't Gorilla vs. Shark because he's asking for very specific information on one very specific feature that is common to both languages, even if it might be implemented differently in both. This is a much more focused question than Gorilla vs. Shark.
    – Evicatos
    Commented Apr 25, 2014 at 17:01
  • 1
    Agreed. Gorilla vs. Shark is about comparing two things that are incomparable, like apples and oranges. Comparing the implementations of a feature in two languages is not Gorilla vs. Shark. It's specific, it's relevant, and the kind of question that experts are interested in. It's not just a "help me choose a language" question. Commented Apr 25, 2014 at 17:02
  • 1
    The reason I mentioned the blog was to round out your understanding of what motivated me to ask the question. I thought it would be a good footnote to to my actual question. I explained some so you wouldn't need to read it yourself, but included a link in case you did. I thought the * and "note:" made that pretty clear, but I guess I should have added some parentheses and a line of dashes, too. PS: sharks.
    – kurofune
    Commented Apr 25, 2014 at 17:08

1 Answer 1

22

In this video I watched recently, Rich Hickey comments that he likes the destructuring part of languages like Scala, but not so much the pattern matching part, and he designed Clojure accordingly. That probably explains why the pattern matching is in a library and not as robust, although the kind of problems seen in the post you mentioned are clearly bugs.

What Rich Hickey mentions as an alternative to pattern matching is multimethods. Most languages let you do polymorphic dispatch based on type. Some languages let you also do it based on a value. Using multimethods, Clojure lets you do it based on any arbitrary function. That's a pretty powerful concept.

It comes down to the principle that programmers using a language should use the language's own best idioms. Trying to write Scala-like code in Clojure is going to have its difficulties, and vice versa.

2
  • Thank you for a very interesting answer to my question. I never thought about multi-methods in that way! It seems it would be overkill for simple tasks but definitely powerful. I agree with what you said about idiom too. I can't wait to get the hang of that in Clojure :)
    – kurofune
    Commented Apr 25, 2014 at 17:22
  • Thanks for this answer. The linked video mentions pattern matching when Rich says "If you have pattern matching envy, this is half of that, except the part of that that I do not like, which is the conditional part." github.com/matthiasn/talk-transcripts/pull/90/files#
    – hawkeye
    Commented Nov 30, 2019 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.