How can I interpret user input as a function in Julia? - julia

I've been using the following function to take in user input for something I'm writing in Julia:
function input(prompt::AbstractString = "")
println(prompt * " ")
chomp(readline())
end
In my particular case, the input that I'm taking in is in the form of equations such as "y = x^2". After the input() function passes it to me as an ASCIIString, I then use the parse() function to convert it to an Expression:
:(y = x^2)
As an Expression, I can use the .args attribute to do things like counting the number of variables and returning the unique variables, all of which has worked fine. Now, I need to be able to evaluate the right side of the expression as the Function f(x) = x^2. To do so, I began writing the following function (which has some pretty major flaws):
function evalExpression()
L = [1,2,3,4]
equation = parse(input("Enter an equation"))
f = equation.args[2].args[2]
for i in L
x = i
value = eval(f)
println(value)
end
end
This function has two problems that I haven't been able to resolve. The first is that it gives me an UndefVarError for x when I try to run it right now; that's more or less expected. The second is that unless I knew that the user would input a function of only x, I would have no way of figuring out what the variables I needed to assign were. I wrote a recursive function that can take in an expression and return all its variables in the form of [:x, :y, etc.], but I cannot assign :x to a number to evaluate the function--I need to assign it just to x, and I cannot figure out how to access that. Is there anything that I can use to access the variables I need? Or a different approach I could take?
Thank you!

When I run the following:
function evalExpression()
L = [1,2,3,4]
equation = parse(input("Enter an equation"))
global x
for i in L
x = i
f = equation.args[2].args[2]
value = eval(f)
println(value)
end
end
and then putting y = x*x I get
evalExpression()
Enter an equation
y = x*x
1
2
3
4
What is missing, at least for x as a variable, is declaring it globally. When you eval parsed statements, these parsed statements only access global variables
So what you probably need to do after you've invented your recursive function to correctly fetch variables, is to create them globally. Maybe
eval(parse("$variable = 0"))
will do

Related

Passing values outside user's defined function in PARI GP

I want to write a function SWAP to swap 2 integers a and b in PARI. This function does not return any values but I want the variables outside the function to store the new values. I don't know how to fix this. This is my function:
swap(a,b) = { t=a; a=b; b=t;}
Then I write:
u=2; v=3;
swap(u,v);
ten u is still 2 and v is still 3. How can I rewrite this function to make u=3 and v=2? Thank you.
Unfortunately, you cannot implement such a swap function for scalar variables. Starting from PARI/GP v2.13.0 you can pass the container variables (like list, vector, etc.) by reference to user-defined functions using ~ prefix in order to modify its components. So you can do simple work-around like the example below for PARI/GP of version >= 2.13.0:
swap(~a,~b) = {[a[1],b[1]] = [b[1],a[1]];};
u = [2]; v = [3];
swap(~u, ~v);
u
> [3]
v
> [2]

Creating an array of `nothing` of any size in Julia

In Julia it is possible to create arrays of any size using the functions zeros(.) or ones(.). Is there a similar function to create an array that is filled with nothing at initialization but also accepts floats? I mean a function like in this example:
a = array_of_nothing(3)
# a = [nothing,nothing,nothing]
a[1] = 3.14
# a = [3.14,nothing,nothing]
I tried to find information on internet, but without success... Sorry, I am a beginner in Julia.
The fill function can be used to create arrays of arbitrary values, but it's not so easy to use here, since you want a Vector{Union{Float64, Nothing}}. Two options come to mind:
A comprehension:
a = Union{Float64, Nothing}[nothing for _ in 1:3];
a[2] = 3.14;
>> a
3-element Array{Union{Nothing, Float64},1}:
nothing
3.14
nothing
Or ordinary array initialization:
a = Vector{Union{Float64, Nothing}}(undef, 3)
fill!(a, nothing)
a[2] = 3.14
It seems that when you do Vector{Union{Float64, Nothing}}(undef, 3) the vector automatically contains nothing, but I wouldn't rely on that, so fill! may be necessary.
I think you are looking for the Base.fill — Function.
fill(x, dims)
This creates an array filled with value x.
println(fill("nothing", (1,3)))
You can also pass a function Foo() like fill(Foo(), dims) which will return an array filled with the result of evaluating Foo() once.

R functions: why expressions can't be evaluated within return?

f=function(x){return(list(y=x, z = y))}; f(1)
I wonder why the above function does not work. Thank you very much!
It doesn't work, because the arguments of the function list cannot "see" each other, so the statement z = y cannot "see" y = x.
This remembers me of the difference of let and let* in Lisp languages.
let does local assignments for local variables. They can't "see" each other. But let* makes the following assignments be able to "see" the previous assignments.
Why not doing:
f=function(x){y <- x; return(list(y = x, z = y))}; f(1)

Accessing values in expression using a macro

I'm wondering whether it's possible to define a macro that can modify the values of an expression only if the values are of a specific type?
Here's a minimal example:
type Special
x::Int
end
f1(s, n::Special) = println("f1", s, n)
f2(s, n::Special) = println("f2", s, n)
x1 = Special(3)
x2 = Special(5)
expr = :(
f1("this is f1", x1),
f2("this is f2", x2)
)
Now a macro might be able to examine the values of the arguments to the functions, determine that x1 and x2 are of type Special, run some function to modify their values, say by changing 3 to 4 and 5 to 2 (it might involve comparing two values), then pass the expression back to the caller. The final result would be equivalent to calling:
f1("this is f1", 4)
f2("this is f2", 2)
I found that it's possible to access the values in a macro via:
eval(eval(filter(x -> typeof(eval(x)) == Special, expr.args[1].args))[1]).x
=> 3
but although this works it looks wrong, and I'm might either be doing it wrong or trying to do something too way out...
No, you should never try to check types or values inside macros. Using eval to figure out the type or value of something in a macro may work in very limited situations, but it'll break in almost every real use. Instead, just have the macro insert a call to a generic function — that's where Julia excels at picking apart types (as method dispatch) and values (within the method):
munge_special(x::Special) = Special(x.x + 42)
munge_special(x) = x
macro do_something_special(x)
return :(munge_special($(esc(x))))
end
julia> #do_something_special Special(2)
Special(44)
julia> #do_something_special 3
3

Creating call objects to compare to formula elements

I would like to create an object from a string to compare with an element of a formula.
For example, in the following:
# note that f does not exist
myForm <- y ~ f(x)
theF <- myForm[[3]]
fString <- "f(x)"
How can I compare fString to theF?
If I know the string is "f(x)" I can manually enter the following
cheating <- as.call(quote(f(x)))
identical(theF, cheating)
which works (it gives TRUE) but I want to be able to take the string "f(x)" as an argument (e.g. maybe it's "g(x)".
The real point of this question is for me to understand better how to work with call objects and quote function.
parse(text = s) converts text, s, to an expression and e[[1]] extracts the call object from a length 1 expression e. theF is a call object so putting these together we have:
identical(theF, parse(text = fString)[[1]])
## TRUE
note that formula's are really nothing on their own in R.
the only thing they do is convert it into a string like object...
"y~f(x)"
it's then on to the functions that accept formulas to interpret it...
check coplot for an example implementation

Resources