Ti-basic passing function as an argument to another function

允我心安 提交于 2020-02-25 06:49:07

问题


In Matlab you can declare an anonymous function and pass it to another function.

[y] = someFunction(@(x) x.^2 , [a bunch of numbers]);

I'd like to do something similar on my TI-89 calculator. I have a function that takes a "math-function" as one of its arguments and I'm trying to do it like this:

myfunction(3/x,1,2)

and my function looks something like this:

myfunction(f,xl,xu)
Func
local a,b
f(xl)→a
f(xu)→b
Return [a,b]
EndFunc

I know I can input my functions in the "y=" editor and then access them inside the function but I would really like to be able to input the math-function directly as an argument. How can I do this?


回答1:


The builtin expr function in TI-BASIC can be used to turn a string into an expression. Here's how to implement your function this way:

myfunction(f,xl,xu)
Func
Local a,b,x

xl→x
expr(f)→a

xu→x
expr(f)→b

Return [a,b]
EndFunc

The call to your function will be myfunction("3/x",1,2). Be sure to enclose the definition of f in double quotes so it is treated as a string.




回答2:


"TI-89 BASIC does not have first-class functions; while function definitions as stored in variables are fully dynamic, it is not possible to extract a function value from a variable rather than calling it. In this case, we use the indirection operator #, which takes a string and returns the value of the named variable, to use the name of the function as something to be passed."

http://rosettacode.org/wiki/Higher-order_functions#TI-89_BASIC



来源:https://stackoverflow.com/questions/23529670/ti-basic-passing-function-as-an-argument-to-another-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!