当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

This is my first post. Please be gentle if in ignorance I violate accepted norms... I did spend significant time in the FAQs. ;-)

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. Thanks!

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

网友答案:

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!

网友答案:

I have played around a little with XSLT, and have delved deeper into the functional languages Lisp, Scheme, and Haskell. From what little I've seen, I'm not sure that XSLT as it's commonly used will teach you that much about functional languages. You may learn a few tricks, but in my opinion, JavaScript is a better analogue to what you'll see in untyped functional languages.

Wikipedia backs me up on this one:

As a language, XSLT is influenced by functional languages, and by text-based pattern matching languages like SNOBOL and awk. Its most direct predecessor was ISO DSSSL, a language that performed the same function for full SGML that XSLT performs for XML. Many of the standards committee that developed XSLT had previously worked on DSSSL, including James Clark. XSLT can also be considered Turing-complete template processor.

In other words, it borrows a lot from functional languages, but also borrows from templating languages like awk and SGML. So yes, it's a functional language, in the same not-entirely-obvious way that C++ templates are a functional language. But there's no easily-mapped line from learning XSLT to Scheme or Haskell.

Also, don't underestimate how different functional languages can be from each other. Lisp and Scheme are both so loosely typed as to feel essentially typeless. I think of these languages as being like the Mandarin Chinese of programming languages, with an essentially fluid, context-based syntax. Haskell, on the other hand, is so strongly typed that it confuses even experienced programmers. It also reaches beyond its lambda-calculus roots to borrow concepts from abstract algebra and Category theory, making it a bit mind-boggling to learn for non-math types.

But none of this is meant to discourage you. By all means, learn a functional language. They will change your approach to programming in ways you'd never suspect.

网友答案:

I'm not quite as qualified as I'm sure some answerers will be. I have used Haskell, Scheme, and XSLT, though I learned them in the opposite order -- Scheme, then Haskell, and then using XSLT for basic stuff. I found that the thinking processes I learned using Scheme and later Haskell definitely helped me understand the high level concepts of XSLT programming. All the details, of course, still had to be dredged from reference guides and such.

I imagine that you'll find the same effect in reverse.

(I also found that XSLT, like other programming-languages-in-XML, is a painful way to process an XML file, and that just writing a script in Python was easier, faster, and more flexible).

网友答案:

I believe XSLT is in fact a good first pure functional language: http://blogs.msdn.com/ashleyf/archive/2007/04/11/xslt-a-pure-functional-language.aspx

分享给朋友:
您可能感兴趣的文章:
随机阅读: