There is no standard way to write custom functions in SPARQL.
SPARQL does support the invocation of custom functions (named by URI), however, and many SPARQL implementations, however, provide a way to extend the set of functions that they include.
answered 30 Jan, 11:07
SPARQL Inferencing Notation, or SPIN (not a standard, but a W3C member submission) lets you define your own SPARQL functions in SPARQL. http://www.topquadrant.com/docs/tbc/AppDevQuickstartGuide/howtospinfunctions.html describes how to do this with TopQuadrant's TopBraid Composer. (Disclaimer: I work for TopQuadrant.)
answered 30 Jan, 12:51
As lee said, there's no standard way to do this, but many tools have their own way to support creation of custom functions. Sesame, for example, allows you to add custom functions in Java - here's an article that explains how (ObDisclosure: I am on the Sesame development team).
answered 30 Jan, 13:54
Jeen Broekstra ♦
As others have said SPARQL supports the notion of identifying custom functions with URIs - see Extensible Value Testing in the SPARQL specification
Again echoing others answers how you do this differs from implementation to implementation:
Disclaimer - I am a developer on both the above projects
answered 31 Jan, 05:45
Rob Vesse ♦