Search code examples
xsltfunctional-programming

Tips & Traps in Learning XSLT and "Real" Functional Programming?


Until recently my knowledge of XSLT was enough for my simple, straightforward needs. But a more advanced requirement sent me to reference material when "just trying stuff" didn't work. It was actually fun starting over at the beginning. I initially learned XSLT in the middle of deadline pressure and in 20/20 hindsight "working" was clearly more important than "understanding."

I was surprised to read on page 33 of Michael Kay's book that XSLT has its roots in Scheme. Wikipedia says,

Scheme is one of the two main dialects of the programming language Lisp.

Maybe I know (or will soon know) more about functional programming than I thought I did. Searching SO for posts like Is XSLT a functional programming language? and following its working links show there are several opinions whether or not XSTL is a functional language. From the post Which functional programming language should I choose as first functional programming language? I got the advice to learn F# first because I come from .NET and I like to get to useful programs quickly.

My first question is, if someone knows XSLT 2.0 very well, how much of a "real" functional programming language such as F#, Lisp or Haskell does the person already know before learning F#, Lisp or Haskell? For purposes of discussion assume a person who "Knows XSLT 2.0 very well" learns very few new concepts the first time she reads the XSLT Cookbook. Instead her reaction to unfamiliar information is, "Of course that's a good way to do that. I should have thought of that!"

The second question is, are there parts of XSLT 2.0 / XPATH 2.0 I should make sure I understand very well to make learning F# / Lisp / Haskell easier?

Third, are there parts of XSLT that are best ignored if one plans to eventually learn F# / Lisp / Haskell / etc.? That is, does XSLT have concepts analogous to GOTO, ALTER and ON ERROR RESUME NEXT that are not appropriate in "real" functional programming languages?

Brief answers are fine because learning a functional programming language is an unscheduled future project for me.

P.S. Several hyperlinks removed for lack of reputation. Drat! ;-)


Solution

  • My advice would be not to lean too heavily on XSLT when you are learning your first second functional language. Here's what I'm thinking:

    • There are now a great many good "on-ramps" to various functional languages that don't presume previous knowledge and experience. If your XSLT experience enables you to travel faster up the on-ramp, we're all surprised and pleased, but it's not necessary.

    • Functional programmers care a lot about syntax. XSLT programmers are forced to use XML syntax. There is therefore a big cap in community norms and values between the two communities.

    • The typical functional language is not only functional, but has something special it cares about, which gives the language its own unique flavor. Some examples (in alphabetical order):

      • Erlang cares a lot about reliability and parallel/distributed computation.
      • F# cares about interoperating nicely with the .NET framework and type system.
      • Haskell cares about purity, about sexy types, and about one conception of beautiful syntax
      • Scheme cares about extensibility and about an entirely different conception of beautiful syntax.
      • XSLT cares about pattern matching and processing XML.

    There's plenty of material on SO and elsewhere about picking a functional language to learn; if you like, you can see what one polyglot functional programmer thinks. Pick a language—any language—and sit back, get to work, and enjoy the flight!