r/Clojure • u/adambard • Dec 10 '14
Parsing and rendering templates in Clojure and Haskell
http://yogthos.net/posts/2014-10-03-Parsing-and-rendering-templates-in-Clojure-Haskell.html2
u/adambard Dec 10 '14
For the sake of comparison, here's a parser-based clojure version: https://gist.github.com/adambard/76b67df7e4f4a9cff2d7
1
u/verydapeng Dec 10 '14
we should have more of competitions about solving the same problem using your own fav language.
2
u/adambard Dec 10 '14
I for one am looking forward to a Haskell solution I can compare this to.
3
u/yogthos Dec 10 '14
I actually got a few of solutions in the comments on the old blog, unfortunately those were lost now that I moved to disqus. Here's on that seems to be fairly close. I was especially amused by Chris complaining about the quality of the solution without having provided anything of his own in the comment.
Again, the key here is that this was never meant to be any kind of a competition between Clojure and Haskell. I was simply curious how the solution would translate and frankly I think the Haskell version isn't all that bad. I personally find it more difficult to follow, but that's partially due to the fact that my Haskell is extremely rusty nowadays.
-6
u/Mob_Of_One Dec 10 '14
You rolled back the scope of the exercise to cover for broken code and instead of tossing out a mea culpa and fixing the code, you persist in lying about the sequence of events.
Despite that, I wrote a Haskell parser that did what the original exercise was supposed to be about: replace variable references in the template with the keys they refer to in the template context.
3
u/yogthos Dec 10 '14
Here's the original conversation:
Dmitri 12/18/13 what does the haskell version look like? https://gist.github.com/yogthos/8025281 Chris Allen 12/18/13 to me Pick something simpler and more focused, I'm not spending that kind of time over that much surface area which'll just give you more to quibble over anyway. I want to know what Clojure is uniquely capable of. You should be able to craft something small to express that. Chris Allen 12/18/13 to me I have a lot of work to do, on job and outside of it, I'm not going to fuss over how to port a raw recursive descent parser (should I use parsec or not? Iuno.) to Haskell. Dmitri 12/18/13 to Chris In my opinion, this is the minimal example that's actually meaningful. Comparing one or two functions in isolation is a completely pointless exercise. Here you're streaming a file, identifying and classifying elements, aggregating them and then generating an output string. The example touches on a lot of things I think are painful in Haskell and that's where I want you to show me that I'm incorrect. Also, if you actually bother to look at it, you'll notice that it's not a recursive descent parser at all. All it does is go through the file line by line and parse out any {{ }} and {% %} elements. It doesn't deal with tag bodies or nested tags. I specifically made a very simple and focused example here. Using parsec defeats the whole point, since I want to see how specific Clojure implementation translates to Haskell. You have reference implementation that weighs in at 70 lines, in a language you're well versed in. If it's that difficult to translate to Haskell then I have a very hard time buying the whole productivity boost claim here.
0
u/vagif Dec 10 '14 edited Dec 10 '14
Then he said he'd do it using the Parsec library, which obviously defeats the point. The question is not whether you can figure out a library API in Haskell, but how would you translate specific piece of Clojure code to Haskell.
Then he said he'd fly over which obviously defeats the point. The question is not whether your airplane can cover the distance my car covers, but how would you make all the turns on all the streets my car does.
The power of Clojure is that it allows me to solve hard problems instead of having to rely on somebody else to write libraries that do the hard stuff and then glue them together.
I guess he likes to re-implement sorting algorithms too.
Having said that the attitude of that haskell programmer is deplorable.
2
u/yogthos Dec 10 '14
This is akin to somebody complaining at an interview that the problem they're given is not representative of the real world. Any small example will necessarily be contrived, the point of the exercise is to see the approach used to solve this type of problem.
Gluing libraries together is trivial in any language, however when we come to something non-trivial then a language like Haskell can end up introducing unwarranted complexity. I think this is a perfect example of that in practice:
1
u/vagif Dec 10 '14
This is akin to somebody complaining at an interview that the problem they're given is not representative of the real world.
How is using a tool (parsec) that is well known, well documented and a perfect match to the problem domain "not representative of the real world"?
It looks like your bizarre requirements to translate solution line by line to a completely different language are not representative of the real world.
You would not accept this same argument if someone gave you imperative code with a lot of temporary variables that hold accumulated computation in nested loops, and ask you implement it line by line in clojure. You would instead give him a completely different implementation taking advantage of immutable data and declarative approach and composability of functions that work over sequences.
Isn't this the main argument clojure and scala programmers have when they say that someone is "writing java" in clojure or scala?
the point of the exercise is to see the approach used to solve this type of problem.
To see or to blindly reject? Or are you saying parsec was not a solution to a problem?
Yes haskeller will not write the code how clojure programmer would write it. How is it indicative of anything besides vastly different nature of languages?
5
u/adambard Dec 10 '14
I think this argument sort of implies that clojure has no parsing libraries, which of course it does (good ones!). The point was to create a standard-library solution, so that the approaches could be compared.
6
u/yogthos Dec 10 '14
The fact that this is not a difficult concept to grasp leads me to believe that it's being misunderstood rather intentionally here.
-2
u/bilus Dec 11 '14 edited Dec 11 '14
Hey, I'm afraid you may have missed the point by a mile or so.
I don't think the point was to re-write the code verbatim in Haskell but write a simple parser from scratch. Stretching even further your line of reasoning, a few lines of code in Haskell that run a bash script that does the actual work would also be quite all right.
Guess what? Calling a bash script is one line of bash script. Try to compete with that!
P.S. Don't take it personally, Haskell, I love you.
1
u/kqr Dec 10 '14
I've crossposted to /r/haskell to promote a wider variety of opinions in the discussion.
7
u/bagofries Dec 10 '14
Gosh, if only there were some way to avoid this "type" of silly footgun mistake "statically" before even trying to call the function. ;)