Io language 'apply arguments'

房东的猫 提交于 2019-12-05 11:11:27

Thanks to that person who suggested evalArgs (not sure where your comment went).

Anyway that has resolved for my situation, although unfortunately not in general I guess.

You can achieve what I describe by doing :

writeln(call evalArgs join)

This evaluates all arguments, and then joins the results into a single string.

draegtun

is there an equivalent to lisp's apply function?

Have a look at perform & performWithArgList methods.


To step back a bit you can replicate Lisp's FUNCALL in a few different ways in Io:

1 - Passing a function ie. block():

plotf := method (fn, min, max, step,
    for (i, min, max, step,
        fn call(i) roundDown repeat(write("*"))
        writeln
    )
)

plotf( block(n, n exp), 0, 4, 1/2 )

2 - Passing a message:

plotm := method (msg, min, max, step,
    for (i, min, max, step,
        i doMessage(msg) roundDown repeat(write("*"))
        writeln
    )
)

plotm( message(exp), 0, 4, 1/2 )

3 - passing the function name as a string:

plots := method (str, min, max, step,
    for (i, min, max, step,
        i perform(str) roundDown repeat(write("*"))
        writeln
    )
)

plots( "exp", 0, 4, 1/2 )


So from all this you could create a Lisp APPLY like so:

apply := method (
    args := call message argsEvaluatedIn(call sender)
    fn   := args removeFirst
    performWithArgList( fn, args )
)

apply( "plotf", block(n, n exp), 0, 4, 1/2 )

apply( "plotm", message(exp), 0, 4, 1/2 )

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