Benjamin W Herta/Fishkill/IBM@IBMUS wrote on 05/12/2012 05:12:24 AM:
> Benjamin W Herta/Fishkill/IBM@IBMUS
> 05/12/2012 05:12 AM
>
> Please respond to
> Amihay Vinter <amiwin...@gmail.com>; Please respond to
> Mailing list for users of the X10 programming language <x10-
> us...@lists.sourceforge.net>
>
> To
>
> x10-users@lists.sourceforge.net
>
> cc
>
> Subject
>
> [X10-users] X10: Performance and Productivity at Scale: Recursive
> Local Functions & Variables
>
> This is an enquiry email via http://x10-lang.org/ from:
> Amihay Vinter <amiwin...@gmail.com>
>
> Hi,
>
> I have started working with the X10 language and had a question,
> hope you can help me...
>
> At the guide,
> http://x10.sourceforge.net/documentation/languagespec/x10-latest.pdf
> at page 206 (at the end)
> it is writen that::
> ----------------------
> "Example: Note that recursive definitions of local variables is not
allowed.
> There are few useful recursive declarations of objects and structs; x, in
the
> following example, has no meaningful definition. Recursive declarations
of
> local functions is forbidden, even though (like f below) there are
meaningful
> uses of it."
> ----------------------
>
> My question is:
> 1. Why doesn't it make sense to have recursive local variables
Look at the example, where we define x to be x+1. It's the same x in both
cases. There's no sensible integer like that. Other definitions like
this
include:
// A Boolean which is its own opposite
val b : Boolean = ! b;
// This ought to be an infinite string of '.'s:
val s : String = s + ".";
An equally troublesome case is when the equation has more than one
solution:
// Any integer works:
val n : Int = n;
// Any even integer works:
val m : Int = m & 0;
> 2. Why does it make sense to have recursive local functions declarations?
Recursive *local* function declarations can make sense, in much the same
way
that recursive methods make sense. Here's an example of a class which has
two
methods to compute Fibonacci numbers, one done as a member and the other
done
as a (not legal X10) recursive local function definition.
class TwoFibonaccis {
public def memberFib(n:Int) = (n < 1) ? 1 : memberFib(n-1) + memberFib
(n-2);
public def funFib(n:Int) {
// Not legal X10
val locFib = (k:Int) => (k < 1) ? 1 : locFib(k-1) + locFib(k-2);
return locFib(n);
}
}
> 3. What are the difficulties in implementing nested functions and
> procedures
There aren't fundamental difficulties. Lots of languages have nested
functions and procedures.
C doesn't have them because ... I guess the designers of C were trying to
keep
it small and easy to compile.
Java doesn't have them because Java (v. 1.6) doesn't even have functions
and
procedures per se. It has methods, which do about the same thing when you
program with them, but it makes less sense to nest methods than it does to
nest functions. (A method is a message which can be sent to the object
'this'; it's conceptually attached to the object or the class or
something.)
X10 has nested functions. twice(f) is a function which applies f to its
input, twice. So twice(square) is the fourth-power function, and twice
(add1)
adds 2, if those functions do what their names say they ought to.
val twice = (f: (Int=)>Int) => (x:Int) => f(f(x));
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users