[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Order of Evaluation

From: fi
Subject: Re: Order of Evaluation
Date: Tue, 20 Aug 2019 11:21:24 +0200
User-agent: Mutt/1.5.24 (2015-08-30)

On Mon, Aug 19, 2019 at 06:37:41PM -0400, Przemek Klosowski via Help list for 
GNU Octave wrote:

> On 8/19/19 2:56 PM, address@hidden wrote:

> >y = @(x) H = h(x), f(H) + g(H); % also wrong if in brackets
> I thought it would work to create a 'fake' compound statement:
> y = @(x) if true  H = h(x), f(H) + g(H) end;
> but this somehow fails, as if the conditional was not evaluated---the
> function does not return anything.
> Given that  "if true 123 end" returns 123, and "y = @(x) 123 ;" returns a
> function returning 123, why does
> y = @(x) if true 123 end ;
> fail to return anything? (Octave 4.2.2 on Linux)

I think Octave does this because Matlab does it. 

It seems, that only real expressions (that are composed from operator
which really return a value) are allowed in anonymous
functions. The if clause seems to be interpreted as a statement which
does not return anything. There are other examples: the statement 

y = x = 3; % will fail in Matlab, because assignment is a statement
           % and not an operation. 

In Octave the assigment is an operation which returns a value and so
Octave accepts multiple assignment. 

> In any case, suggests
> that anonymous functions aren't universally useful and recommends simply
> using function handles.

yes, but in this case my expressions are basically simple and I try to
increase the performance (which I believe is an advantage of anonymous

Best Regards

Torsten Finke

Dr.-Ing. Torsten Finke
GnuPG-Key: 1024D/8F2300D8
Fingerprint: B929 7FA5 4D2E E9B6 C55C  8A0B 7DF4 86E9 8F23 00D8

reply via email to

[Prev in Thread] Current Thread [Next in Thread]