Evaluating a local function within the global namespace

Hi. I’ve written a tutoral program in which students submit an answer ans to an exercise using the format reply(ans). reply then passes ans deeper into a module within which ans is tested for correctness. The thing is, that ans might well contain variable names or functions that only exist in the Main namespace, and so I want - within my own module - to evaluate ans within the context Main. I tried something like this:

module Mod

export reply

iscorrect = (ans -> ans==myvar)

function reply( ans)
# Alternative 1:
iscorrect( Core.eval(Main,:ans))
# Alternative 2:
Core.eval(Main,:(iscorrect( ans)))
end

end # of Mod

using .Mod
myvar = 3
reply( 3)

I now have the two alternatives shown in the body of reply(): I can either evaluate :ans in Main or I can evaluate :(iscorrect(ans)) in Main. In the first case, iscorrect() doesn’t know about the existence of myvar, and in the second case, Main doesn’t know anything about the existence of iscorrect().

Clearly, in this example case, I could insert a Main evaluation of myvar within the mapping iscorrect; however in the tutorial program, iscorrect is just one of hundreds of correctness tests to be applied appropriately to different exercise replies.

Does anyone have any ideas how I could achieve this?
Thanks! :smile:

I’m a little confused by this example code. In this example ans is just a value—3 and myvar isn’t passed by the student at all so the checking code can’t know about it. How do you know that iscorrect is supposed to compare ans with myvar?

1 Like

An exercise requires from the student that she does certain things then reports back on the results. A typical exercise might be that the system asks her to create a vector myvect = collect(7:11) and reply with the value of the component myvect[3]. She enters reply(9), and the program checks whether this is indeed the third component of myvect.

I realise that this seems a little contrived in the context of my toy example, but in general, I want to be able to verify the correctness of arbitrary things she has tried out, and this involves evaluating expressions within her Main environment.

There is some reason to be so strict on how the things get done, instead of just making a test suit that tests if functions defined by the students always give the right answers given the correct inputs?

This is:

module Mod

export check_square

function check_square(f)
    for i in -10:10
        # Probably using @testset and @test from module `Test` is better
        @assert f(i) == i ^ 2 
    end

    ... # Other tests for each function.
end

end

using .Mod

f(x) = x^2

check_square(f)

Maybe you need to do more complicated things but for this example it doesn’t look like you need eval.

module Mod

export reply
iscorrect = (ans -> ans == Main.myvar)
reply(ans) = iscorrect(ans)

end # of Mod

using .Mod
myvar = 3
reply(3)
2 Likes

:smiley: Oh, how glad I am that this forum exists! Gunnar, you’ve saved my life! The students found the bug today and I need to fix it this evening. I would still like to hear if anyone has an answer to my original question, but in the meantime you’ve given me a fix that will get things moving in time for tomorrow. Thanks!