Show HN: PDF parsing in Racket, my weekend project
planet.racket-lang.org
planet.racket-lang.org
Racket really deserves a lot more recognition and visibility: the language builds on the traditional strengths of Scheme and adds multiple compatible and innovative language dialects, first-rate documentation, extensive libraries, a great IDE and other tools, along with an active and enthusiastic community.
I really hope Racket grows in popularity and we keep seeing more stories like this, as the developers deserve praise for what they have created. And of course, HN is built on it too!
I don't have a CompSci background, which may have made things more difficult, but I know Haskellites who are computer scientists, and have stronger mathematical backgrounds than I have, and many of those guys told me it took months of effort and several attempts before they were truly comfortable with the language.
In contrast, Racket feels like it's on my side, like it will help me to grow as a programmer rather than forcing me to grow on my own. I was able to start writing useful (but simple) stuff almost immediately, but at the same time there's plenty of scope to learn more sophisticated techniques and approaches later on. There's even a typed Racket dialect (http://docs.racket-lang.org/ts-guide/index.html)!
I may look at Haskell again in the future, but at the moment I'm very happy with Racket.
It took me about 4-5 months to get comfortable with Haskell, and a couple of years to make it the language I'm most comfortable in.
GHC type errors can also be very frustrating.
But I agree with dons that unless you're doing very advanced "type hackery" at the edges of what it can do, you really aren't "fighting the type system", just fighting the difficult error messages.
I do find that months of investment into a crucial tool such as a programming language is worth it for anyone who careers in programming.
I can testify that with time, the amount of type errors becomes much smaller, and that you start to smell GHC error messages a mile away :) That is, when seeing an error message, the cause is usually immediately clear simply by having been trained to see that same error message many times.
When I do rarely happen to struggle with a type error, it is a real bug, whose debugging in many cases would have been more frustrating in run-time.
It took me about 4-5 months to get comfortable with Haskell, and a couple of years to make it the language I'm most comfortable in.
I can see your point and I know that lots of people are doing great things with Haskell, but I simply made the assessment that for me, right now, I don't want to put in hundreds more hours of effort into learning Haskell, just to see if maybe I can finally get to the point of achieving enlightenment and maybe even build something useful one day.
That's why I'm so excited about discovering Racket; at first, I simply thought of it as a stop-gap on the road back to Haskell. But as I've learned more about it, I see that Racket has got so much to offer in its own right that I don't necessarily need to go back at all; I'm achieving enlightenment already!
I do find that months of investment into a crucial tool such as a programming language is worth it for anyone who careers in programming.
I agree; I'm going to put that effort into Racket. I'm a geneticist who codes a lot at work. Perl, R, shell and a bit of C remain my workhorse languages for this, but I've gradually developed an interest in programming more generally, and I'm already building more and more new stuff in Racket while taking a more rigorous approach to the design process.
Matthias summarized this pretty well[1].
[1] http://www.ccs.neu.edu/home/matthias/Thoughts/Racket_is____....
Racket code doesn't look like Haskell, the MLs, or even dynamically typed "functional" languages (like Erlang). It looks like LISP. What drives your coding is not functional abstractions or object-oriented data structures (which it can do equally well) or anything like that. What drives your coding is the fact that syntax itself is a first-class data structure. You have access to the reader. You can write macros that adapt the language to anything you want. You can write a DSL in a few hundred lines that might save you tens of thousands of lines.
Now, Racket is certainly good for functional programming. In fact, some Racket developers prefer the Scheme-style tail recursion method of iteration (via the named let or letrec) to the looping constructs provided in the library, even when for loops would be just as effective. In the same way, not all Common Lisp programmers like the loop macro, and some (e.g. pg) actually use Common Lisp in a style that resembles functional programming. However, don't think of Racket as a functional language. That's as misleading as calling C++ a procedural one, even though you could write all your code C-style without ever using objects. Racket is a LISP, which means it can be adapted to fit virtually any paradigm. Racket is far closer to Common Lisp and Clojure than it is to literally any non-LISP.
I'm saying exactly that. Qi/Shen are basically Haskell with parentheses but because of those parens we call them Lisps. My Scheme->x86 compiler uses an s-expr assembly language as its final IR. That language has about as much in common with Lisp as C does.
Another point: yes, Racket programmers know and use tail-calls, but that has nothing to do with "the looping constructs provided in the library" since those are implemented in terms of the same facility. The existence of these loops is therefore not making the language any less functional than the fact that you can implement a while loop in Haskell. The bottom line is that Racket is as functional a language as the interpretation of the term was before Haskell kidnapped it and turned it into some religious point.
(BTW, if you want to bash lisps, do yourself a favor and drop the all-caps "LISP" -- it immediately demonstrates the kind of limited knowledge you have on it.)
As for your parenthetical add-on, I'm not bashing LISPs at all. I think Racket is a beautiful language, in part precisely because it has the power of a LISP dialect. As for your second point, using the capitalized form is the only unambiguous word that refers to the family because many developers in the Common Lisp community use Lisp to mean CL. The reddit style "do yourself a favor" and ill-formed judgments about people's "limited knowledge" are way less constructive than asking "why did you use that spelling." Please keep discussions on HN objective and civil.
In fact, if you want to focus on macros as some kind of a driving force for code -- then it that exact aspect (a) macros in Racket can be very different than macros in other Lisps; (b) more than that, there are many kinds of macros in Racket that you cannot write in those Lisps. As for digging on-line for crossover code from other Lisps that finds its way into Racket: you'll obviously find a lot of Scheme code, but practically nothing from other Lisps. The bottom line is that the syntactic "lots of parens" similarity is an extremely shallow one.
Bottom line: Racket is roughly at the same level of a "functional programming language" as ML etc, certainly more than Python and Ruby where side-effects are embraced much quicker. Like you said, "even have equivalents of map..." -- whereas in Racket these kind of functional/non-destructive operations are expected. (For example, the Racket GC is tuned to perform well when allocating lots of short-term objects, something that is a direct result of FP being the most dominant style.)
And yes, I know that you're not bashing Lisp -- you're just quick to lump all Lisps on the same pile, and reach the obviously bogus conclusion that first-class syntax is the thing that drives code. (That's a point that is subjectively obvious to me, as someone who has been in this part of the PL world for more than two decades.) "LISP" is, BTW, just an outdated spelling, period. It's true that in CL circles "Lisp" is taken as implicitly meaning "Common Lisp", but in the same circles "LISP" is taken implicitly as "an outdated spelling for Lisp, therefore Common Lisp" unless you're one of the old farts whose making a reference to LISP 1.5 or something as ancient.
One thing that I've been using Racket for is to make research posters for conferences. Racket has an excellent library for functional picture/slideshow composition; you can read about that here (which doubles as a great intro to racket in general): http://docs.racket-lang.org/quick/index.html
It's sort of like a "LaTeX for pictures"; where you can say
(vc-append (square 10) (circle 10))
to have a 10px square sitting on top of a 10px circle (vertical, centered). Once you build your poster this way, you can save it as a PDF. This is geat for having perfectly aligned blocks of text sitting in perfectly spaced colums, for example. It's much better than fiddling with the layout manually in powerpoint.However, in designing my poster, I have to include PDF figures. Racket didn't include a way of rendering PDFs, so in my last poster, I had to use 600DPI bitmaps of my figures, which was slow and made the file terribly huge. This library binds to libpoppler, which is great because Racket's native pictures are Cairo-backed anyway, and Racket's FFI is top notch (once you can figure it out). Now I can use the usual functional composition to add these PDF figures to the rest of my poster.
now it might still be a little harder to do this sort of thing in haskell, which is aggressively pure, simply because some algorithms are pure from the outside but have internal steps that involve mutating data for efficiency. but racket is not a pure functional language; if you need to, say, transform an array in place rather than take an array and return a new one, it will not stand in your way. the difference between racket and, say, java, is not that it enforces functional over imperative programming, but that it makes functional programming a lot easier, and fp has a lot of powerful tools in its toolbox for tackling this class of problem.
[1] http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pd...
- To my knowledge, there are no sane C-based PDF renderers with a permissive BSD-like license
- Poppler renders to Cairo --- a _huge_ win because I don't actually have to do anything to convert Cairo surfaces to Racket's native drawing type
- I don't know / don't have the time to implement my own PDF reader/parser from scratch.