Categories
Technology

Now how do you do this in functional programming

Consider the following piece of code:

...
int flag=0;
for(int i=0;i<10;i++){
   if(val==arr[i]){
      flag=1;
      break;
   }
}
if(flag==1){
   System.out.println("Found.");
}
else{
   System.out.println("Not found.");
}
...

In functional programming, we cannot assign values to variables; variables can only be initialized. So I can't use a 'flag', the reason being that if I initialize the 'flag' outside the for, I can't change its value inside the for and if I initialize the 'flag' inside the for, I can't use it outside because of scoping.

For people new to functional programming, here's an excerpt from Wikipedia:

Functional programming languages have the following features (or should I call it restrictions?):

...Functional programming can be contrasted with imperative programming. Functional programming appears to be missing several constructs often (though incorrectly) considered essential to an imperative language such as C or Pascal. For example, in strict functional programming, there is no explicit memory allocation and no explicit variable assignment. However, these operations occur automatically when a function is invoked: memory allocation occurs to create space for the parameters and the return value, and assignment occurs to copy the parameters into this newly allocated space and to copy the return value back into the calling function. Both operations can only occur on function entry and exit, so side effects of function evaluation are eliminated.

I had such a requirement in my program. I was using XSLT to program something and I came across this requirement. (XSLT is also a functional programming language). I then used some XPath constructs to solve it but wondered if there is a standard way to solve it in functional programming languages.

I am new to functional programming (although I have been doing XSLT scripting for the last 1 year) and I am still looking for a standard solution to this pattern.

Categories
World Wide Web

The default namespace problem in XSLT 1.0

(This blog is only for my reference. Non-XSLT users please dis-regard
this blog entry. XSLT users please suggest some alternatives/comments.)

There is a problem popularly called the default namespace problem in
XSLT 1.0. Supposedly this problem has been solved in the newer version
of XSLT (XSLT 2.0).

The problem is not discussed here, rather the solution is given below:

Consider an input XML file bound to a default namespace:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="new.xsl"?>
<catalog xmlns="http://buzypi.50webs.com/">
 <entry>1</entry>
 <entry>2</entry>
 <entry>3</entry>
</catalog>


Consider the XSLT style-sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://buzypi.50webs.com/"
>

<xsl:template match="/">
 <html>
 <body>
  <xsl:apply-templates select="my:catalog/my:entry" />
 </body>
 </html>
</xsl:template>

<xsl:template match="my:entry">
 <h1><xsl:value-of select="." /></h1>
</xsl:template>

</xsl:stylesheet>

The problem is thus solved by defining our own namespace and using it to refer to all default namespace elements.

There is another solution using local-name() function of XPath (Please refer to biglist link below for that solution).

XSLT 2.0 has something called default-xpath-namespace, which is used to solve the problem.

This thread on biglist deals with the default namespace problem.