Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!postnews.google.com!r34g2000vba.googlegroups.com!not-for-mail
From: Bruno Luong <b.luong@fogale.fr>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Recursive anonymous function
Date: Mon, 9 Mar 2009 05:41:54 -0700 (PDT)
Organization: http://groups.google.com
Lines: 52
Message-ID: <8c4a1f3e-4b46-4e60-b2e8-93dc78a95678@r34g2000vba.googlegroups.com>
References: <gp22jh$sif$1@fred.mathworks.com> <gp2in5$pro$1@fred.mathworks.com> 
	<3263fe37-5f71-4844-ac96-69fd61d87634@x13g2000yqf.googlegroups.com> 
	<gp30n9$l1o$1@fred.mathworks.com>
NNTP-Posting-Host: 90.48.42.244
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1236602515 21207 127.0.0.1 (9 Mar 2009 12:41:55 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 9 Mar 2009 12:41:55 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: r34g2000vba.googlegroups.com; posting-host=90.48.42.244; 
	posting-account=q4zNLgoAAACH8apwmoMBTJWEoI6togYL
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.7) 
	Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:523455


On Mar 9, 1:06=A0pm, "Brandon"
<bnavra.remove.t...@student.usyd.edu.au.remove.this> wrote:
> Bruno Luong <b.lu...@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 =3D sumv()
> =A0 =A0 x =3D 1:5;
> =A0 =A0 i=3D5;
> =A0 =A0 iff =3D @(cond,t,f)((cond=3D=3Dtrue).*t + (cond=3D=3Dfalse).*f);
> =A0 =A0 sumval =3D @(x,i)(x(i) + eval(char(iff(i=3D=3D1,'0', 'sumval(x,i-=
1)'))));
> =A0 =A0 res =3D 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 poin=
t to it.
> I am experimenting with matlab and trying to learn some of its limitation=
s.
> 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 i=
t seems odd that the code works inside another function.
>
> Brandon

You are right, I shouldn't discourage you to pursue of exploring. If
you want to know how MATLAB captures the workspace context, use
function FUNCTIONS on anonymous function.

Good luck,

Bruno