question on alternate function declaration syntax

R

rcallan

I've always wondered why there is no language (to my knowledge) where a
function could be declared and used like this:

declaration:
int[] find(int numOfRoots)RootsOf(String eqn){...}


invocation:
int[] solns= find(2)RootsOf("x^2");

If no spaces are allowed, this syntax seems unambiguous, and I can't
think of a reason why it would cause problems (besides it being
unwanted or not useful:))

Is there a reason why this isn't allowed? Would it even be useful?
Thanks,
Rob Callan
 
P

Peter Davis

declaration:
int[] find(int numOfRoots)RootsOf(String eqn){...}

invocation:
int[] solns= find(2)RootsOf("x^2");

If no spaces are allowed, this syntax seems unambiguous, and I can't
think of a reason why it would cause problems (besides it being
unwanted or not useful:))

Firstly, most people wouldn't like to have no spaces at all, and some
language grammars are written in a way that having no spaces between
tokens is simply impossible.
Is there a reason why this isn't allowed? Would it even be useful?

Functional languages do already support this style. For example, in Haskell,

find :: Int -> (String -> [Int]) -> String -> [Int]
find n f s = take n (f s)

This is a function that takes three arguments: the number of items to
find (2), another function that maps a string to a list of ints, and
then the string to apply to the function. So to call:

find (2) RootsOf ("x^2")

is just passing three arguments to the "find" function. RootsOf would
be defined however you like, and you could easily apply other functions
to "find" other than RootsOf.

So yes, there most certainly is a use for meta-functions like you
describe, but their use is completely undefined in Java since methods
are not first-class values. (The java.lang.reflect.Method class gains
you something, but it's hardly a substitute for a real functional
language.) Check out Haskell or OCaml if you want to learn more.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top