0

What benefit does it get from treating functions specially? For example,

function n = f(x)
   2*x
endfunction

f(2) //outputs 4
f = @f
f(2) //outputs 4

If handles can be called the same way as functions, then what benefit do we get from functions being treated specially. By specially I mean that variables referring to functions can't be passed as arguments:

function n = m(f,x)
   f(x)
end

m(f,2) // generates error since f is called without arguments

Why aren't functions procedures (which are always pointed to by variables) like in other functional languages?

EDIT: It seems like my question has been completely misunderstood, so I will rephrase it. Compare the following python code

def f(x):
    return 2*x
def m(f,x):
    return f(x)
m(f,3)

to the octave code

function n = f(x)
   2*x
end
function n = m(f,x)
    f(x)
end
m(@f,2) % note that we need the @

So my question then is, what exactly is a function "object" in octave? In python, it is simply a value (functions are primitive objects which can be assigned to variables). What benefit does octave/matlab get from treating functions differently from primitive objects like all other functional languages do?

What would the following variables point to (what does the internal structure look like?)

x = 2
function n = f(x)
   2*x
end
g = @f

enter image description here

In python, you could simply assign g=f (without needing an indirection with @). Why does octave not also work this way? What do they get from treating functions specially (and not like a primitive value)?

samlaf
  • 425
  • 4
  • 9
  • https://www.quora.com/Is-MATLAB-Octave-a-functional-programming-language – Vahe Tshitoyan May 23 '17 at 22:13
  • your last example gives an error because `f` isn't defined when you call `m(f,2)`, not because `f` has no arguments. If instead you did `f = x.^2; m(f,2)` you would get the result `4` as expected. It's currently unclear what sort of answer you are looking for... – Wolfie May 24 '17 at 08:37
  • f was obviously referring to the function defined in the previous block of code... I just wanted to save space. I edited my question, hope what I'm asking for is clearer. – samlaf May 24 '17 at 14:29
  • Related/interesting: [What is a function handle and how is it useful?](https://stackoverflow.com/q/796935/52738) – gnovice May 24 '17 at 14:37

2 Answers2

1

Variables referring to functions can be passed as arguments in matlab. Create a file called func.m with the following code

function [ sqr ] = func( x )
    sqr = x.^2;
end

Create a file called 'test.m' like this

function [ output ] = test( f, x )
    output = f(x);
end

Now, try the following

f=@func;
output = test(f, 3);
Vahe Tshitoyan
  • 1,439
  • 1
  • 11
  • 21
0

There's no "why is it different". It's a design decision. That's just how matlab/octave works. Which is very similar to how, say, c works.

I do not have intricate knowledge of the inner workings of either, but presumably a function simply becomes a symbol which can be accessed at runtime and used to call the instructions specified in its definition (which could be either interpreted or precompiled instructions). A "function handle" on the other hand, is more comparable to a function pointer, which, just like c, can either be used to redirect to the function it's pointing to, or passed as an argument.

This allows matlab/octave to do stuff like define a function completely in its own file, and not require that file to be run / imported for the function to be loaded into memory. It just needs to be accessible (i.e. in the path), and when matlab/octave starts a new session, it will create the appropriate table of available functions / symbols that can be used in the session. Whereas with python, when you define a function in a file, you need to 'run' / import that file for the function definition to be loaded into memory, as a series of interpreted instructions in the REPL session itself. It's just a different way of doing things, and one isn't necessarily better than the other. They're just different design / implementation decisions. Both work very well.

As for whether matlab/octave is good for functional programming / designed with functional programming in mind, I would say that it would not be my language of choice for functional programming; you can do some interesting functional programming with it, but it was not the kind of programming that it was designed for; it was primarily designed with scientific programming in mind, and it excels at that.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • I don't see how this design decision makes your 3rd paragraph true. Couldn't Python do the same by having a "path" variable and then automatically importing the function definitions it finds in this path. – samlaf May 24 '17 at 23:13
  • Also the difference with C is that in C, the programmer needs to automatically dereference a function pointer in order to be able to use it, whereas this is not the case in Octave. Octave will automatically dereference function handles for you. So my question can be rephrased as: why not make EVERY function just a function handle? Then octave would behave similarly to python and other functional languages. There must be a fundamental (design) reason why they don't/can't do this, but I can't see it... – samlaf May 24 '17 at 23:14
  • "The programmer needs to dereference a function pointer" <-- this is not true. Syntactically, you can use a pointer directly as if it were a function. e.g. if `fptr` is a function pointer, you can call the underlying function as `fptr(args)`, and this is as valid an invocation as `(*fptr)(args)`. Dereferencing _does_ happen under the hood in both cases, of course, just like when a function handle is 'dereferenced' in matlab/octave. – Tasos Papastylianou May 25 '17 at 10:29
  • Python "Importing the function definitions it finds in its path" would still involve them entering the workspace and taking up session memory. Naturally, like you suggest, if you _prefer_ having these functions available as handles on your workspace in matlab/octave, you can convert them to function handles. But otherwise, functions are essentially glorified 'redirection' to external code, handled wholly by the interpreter, but my understanding is that they're not loaded into session memory in the same way as python. (of course, I may be wrong, and an actual octave developer might correct me) – Tasos Papastylianou May 25 '17 at 10:37
  • But you still need to eventually load the functions into memory when you'll use them, so Python could do the same thing (create a table of function names -- those defined with the def syntax -- which will only be loaded when first used). Anyways I guess I'm just uselessly speculating at this point. But thanks for your help :) – samlaf May 25 '17 at 16:34