Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!postnews.google.com!x13g2000yqf.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 04:24:55 -0700 (PDT)
Organization: http://groups.google.com
Lines: 30
Message-ID: <3263fe37-5f71-4844-ac96-69fd61d87634@x13g2000yqf.googlegroups.com>
References: <gp22jh$sif$1@fred.mathworks.com> <gp2in5$pro$1@fred.mathworks.com> 
	<88c4e6f3-3ab8-48cc-9713-d2bcde367719@x13g2000yqf.googlegroups.com> 
	<gp2ooa$40r$1@fred.mathworks.com>
NNTP-Posting-Host: 81.255.204.9
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1236597895 22548 127.0.0.1 (9 Mar 2009 11:24:55 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 9 Mar 2009 11:24:55 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: x13g2000yqf.googlegroups.com; posting-host=81.255.204.9; 
	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)
X-HTTP-Via: 1.1 ftp.nimes.fogale.com:3128 (squid/2.5.STABLE7)
Xref: news.mathworks.com comp.soft-sys.matlab:523444


On Mar 9, 10:50=A0am, "Brandon"
<bnavra.remove.t...@student.usyd.edu.au.remove.this> wrote:

>
> sumval =3D @(x,i)(x(i) + eval(char(iff(i=3D=3D1,'0', 'sumval(x,i-1)'))));
>
>
> As I mentioned previously, there is nothing wrong with the recursion itse=
lf;
> the problem is that it doesn't work unless declared in an m-file

Stop right there. Yes there is something wrong, but you need to know
the way Matlab stores anonymous function to see what is going on.

When parsing the expression with new declared anonymous function,
Matlab captures and stores all local workspace context in a structure-
like. 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.

I recommend to focus your effort on something else rather than abusing
anonymous function and EVAL. Those two are bad enough on it own, let
alone when combining them. I have a hard time to see what advantage
you can draw by programming in that way. Probably the only good thing
is it teaches us how thing work internally.

Bruno