[Scilab-users] Anonymous functions
Pierre Vuillemin
contact at pierre-vuillemin.fr
Fri Jan 20 09:00:39 CET 2017
Hi Samuel,
Indeed, the behaviour I seek to reproduce is similar to what could be
done with a global variable. Yet I would avoid to resort to global
variables.
From what I've understand about variables scoping in Scilab
(https://wiki.scilab.org/howto/global%20and%20local%20variables),
variables from the above level are only replaced/copied locally when
needed, e.g.
function outer()
a = 1
function inner()
disp(a) // a has the value of the outer function
a = 2 // now a copy is made and a is locally equal to 2
endfunction
inner()
disp(a) // a has kept its initial value of 1
endfunction
Hence I guess that the fact that a = 1 in the scope of the outer
function is stored somewhere so that Scilab can retrieve that
information when needed. I'm wondering how it works internally and
whether I could use this to create generic lambda function?
I'm aware that this cannot be done solely in Scilab but requires to
fiddle with the C interface, but I'm curious to experiment.
Regards,
Pierre
Le 17.01.2017 14:21, Samuel Gougeon a écrit :
> 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
>
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
More information about the users
mailing list