[Scilab-users] Anonymous functions

Samuel Gougeon sgougeon at free.fr
Tue Jan 17 14:21:13 CET 2017


Le 17/01/2017 11:46, Pierre Vuillemin a écrit :
> I have actually implemented a similar solution (in the previous link) 
> except that the data are stored in some list. This leads to anonymous 
> functions that behaves as in Matlab, i.e. the data of the function is 
> instantiated when the function is created.
>
> In python, the value of the data 'a' is instantiated when the function 
> is evaluated. Therefore, if the data 'a' changes, the function changes.
>
> Here I could make something like
>
> deff('y=f(x)','y = a*x')
>
> but then the function will get the value of the variable 'a' of the 
> current namespace, e.g.
>
> a = 1
> function outer(x)
>  a = 2
>  disp(f(1))
> endfunction
>
> f(1)     // will give 1
> outer(1) // will give 2
> a = 3
> f(1)     // will give 3
> outer(1) // will give 2
>
> In this example, I would like f(1) to behave identically independently 
> of its position.

If i understand correctly your query, the global space is unique and 
aims to allow that:
deff('y=f(x)','global a; y = a*x')

This requires the only variable "a" you want to consider been declared 
"global a" before some calling point. Actually, i don't see how to refer 
to a unique "a" without telling to Scilab which one it must be.

Samuel






More information about the users mailing list