Exercise 1; 13-Dec-2000

Knowledge Markup Techniques; Winter 2000

Harold Boley; DFKI, Univ. Kaiserslautern

In XSLT we can define a recursive factorial function as a named template as follows:
  <xsl:template name="factorial">
    <xsl:param name="n"/>
    <xsl:choose>
      <xsl:when test="$n = 0">1</xsl:when>            <!-- factorial(0) = 1 -->
      <xsl:when test="$n > 0">                        <!-- factorial(n) =   -->
        <xsl:variable name="factor">
          <xsl:call-template name="factorial">
            <xsl:with-param name="n" select="$n - 1"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$n * $factor"/>         <!-- n*factorial(n-1) -->
      </xsl:when>
    </xsl:choose>
  </xsl:template>

a) The same program can be written in Relfun (Prolog-like syntax) as

  factorial(0) :& 1.
  factorial(N) :- >(N,0), Factor .= factorial(-(N,1)) & *(N,Factor).
Change this Relfun program such that the auxiliary variable Factor is no longer needed. Hint: Substitute Factor's second occurrence with the value of its first occurrence.

b) Considering an a)-like change also in the XSLT program, what would be required here for a factorial definition without the auxiliary variable factor?

c) Define a recursive factorial function in RFML, MathML, or some other markup language. Hint: RFML markup may be produced automatically from Relfun's Prolog-like syntax via "style xml" of the Relfun system.

d) List three essential features making the XSLT version different (longer or shorter) than your version defined in c).

e) Copy and edit the XSLT version such that it defines the non-recursive function ab2(a,b) := a*a + 2*a*b + b*b. Hint: Consider whether or not you'll need a variable corresponding to factor.

f) Define a function factorialab2 in XSLT, which composes the factorial and ab2 functions as follows: factorialab2(m,n) := factorial(ab2(m,n)). Hint: In Relfun factorialab2 could be defined directly, like in the above nesting, or with an auxiliary variable, as factorialab2(M,N) :- Squaresum .= ab2(M,N) & factorial(Squaresum).