Why could this happen?

I used Expr to express a very very complicated math expression. I used println(io,expr) to put it into a file.
The size of the file is 480KB.
When I opened the file (using vscode,so I’m sure the problem is not here), I found it ended in -, with unclosed (, by the way there’s no new line.

enviroment:

  • windows10
  • julia1.6.1

Without knowing what that expression is, it’s kind of impossible to find out.

1 Like

I’m sorry but it’s too long.

Could you share a working example of the code that generates it?

Can you share how you arrived at that expression? Or maybe upload the code to some third party (e.g. gist.github.com) to not clutter discourse?

# does derivation
function der(a::Symbol)
    if a==:x
	    return 1
    else
	    return 0
    end
end
function der(a::Int)
    return 0
end
function der(a::Expr)
    f=a.args[1]
    if f==:+
	    return Expr(:call,:+,der(a.args[2]),der(a.args[3]))
    elseif f==:-
	    return Expr(:call,:-,der(a.args[2]),der(a.args[3]))
    elseif f==:*
	    return Expr(:call,:+,Expr(:call,:*,a.args[2],der(a.args[3])),Expr(:call,:*,der(a.args[2]),a.args[3]))
    elseif f==:/
	    return :($(der(a.args[2]))*$(a.args[3])-$(a.args[2])*$(der(a.args[3]))/$(a.args[3])^2)
    elseif f==:ln
	    return :($(der(a.args[2]))/$(a.args[2]))
    elseif f==:^
	    return :($(der(a.args[3]))*($(a.args[3])*$(a.args[2])^($(a.args[3])-1)+ln($(a.args[2]))*$(a.args[2])^$(a.args[3])))
    else # can ignore
	    return Expr(:call,0)
    end
end
f=:(x^x^x^x^x^x)
io=open("D:/1.txt","w")
println(io,der(der(der(der(f)))))

I ran the code again and the file stopped at the same place.

Try to add close(io) at the end.

Note that it is recommended to let Julia close the file automatically using the do block syntax:

open("D:/1.txt","w") do io
    println(io,der(der(der(der(f)))))
end

instead of

io = open(...)
println(io, ...)
close(io)
2 Likes