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

Christophe
  • 74,672
  • 10
  • 115
  • 187
kurofune
  • 290
  • 2
  • 8
  • recommended reading: **[Discuss this ${blog}](http://meta.programmers.stackexchange.com/questions/6417/discuss-this-blog)** – gnat Apr 25 '14 at 16:40
  • 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 Apr 25 '14 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. –  Apr 25 '14 at 16:56
  • [Gorilla vs Shark](http://blog.stackoverflow.com/2011/08/gorilla-vs-shark/) -- _"if you... don’t want your question to get instantly closed... — try to keep Gorilla vs. Shark in mind."_ – gnat Apr 25 '14 at 16:57
  • 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 Apr 25 '14 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. – Karl Bielefeldt Apr 25 '14 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 Apr 25 '14 at 17:08
  • 1
    @kurofune it looks like most of us, myself included, agree that it was a well-written question. –  Apr 25 '14 at 17:17
  • 1
    @gnat http://c2.com/cgi/wiki?QuotingNotThinking –  Apr 25 '14 at 17:19
  • 1
    @MattFenwick "It comes down to the principle that programmers using a language should use the language's own best idioms." – gnat Apr 25 '14 at 17:21

1 Answers1

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.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
  • 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 Apr 25 '14 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." https://github.com/matthiasn/talk-transcripts/pull/90/files# – hawkeye Nov 30 '19 at 05:12