Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Recursive anonymous function
Date: Mon, 9 Mar 2009 12:06:01 +0000 (UTC)
Organization: University of Sydney
Lines: 36
Message-ID: <gp30n9$l1o$1@fred.mathworks.com>
References: <gp22jh$sif$1@fred.mathworks.com> <gp2in5$pro$1@fred.mathworks.com> <3263fe37-5f71-4844-ac96-69fd61d87634@x13g2000yqf.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1236600361 21560 172.30.248.35 (9 Mar 2009 12:06:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 9 Mar 2009 12:06:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1308310
Xref: news.mathworks.com comp.soft-sys.matlab:523450


Bruno Luong <b.luong@fogale.fr> wrote 
> SUMVAL statement inside the expression has not yet set (when it
> was declaring), and it cannot be captured. Consequence: the eval
> fails, because SUMVAL is unknown when recursion is expected. End of
> the story.
> 

Bruno,
Please create an m-file and put the following code in it

function res = sumv()
    x = 1:5;
    i=5;
    iff = @(cond,t,f)((cond==true).*t + (cond==false).*f);
    sumval = @(x,i)(x(i) + eval(char(iff(i==1,'0', 'sumval(x,i-1)'))));
    res = sumval(x,i);
end

then run it. I get an answer of 15, which means that the function worked.

> I recommend to focus your effort on something else rather than abusing
> anonymous function and EVAL...I have a hard time to see what advantage
> you can draw by programming in that way.

Please don't be dismissive of my query just because you do not see a point to it. 
I am experimenting with matlab and trying to learn some of its limitations.
I read that anonymous functions can't have if statements nor while loops and I am trying to see if I can prove that otherwise. 

now back to the analysis
you said:
>...new declared anonymous function, 
>Matlab captures and stores all local workspace context...

that implies that anonymous functions are closures. If this is the case it seems odd that the code works inside another function. 

Brandon