# Why could this happen?

**URL:** <https://discourse.julialang.org/t/why-could-this-happen/68128>\
**Category:** General Usage\
**Created:** [September 14, 2021, 4:09am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128 "2021-09-14T04:09:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [September 14, 2021, 4:09am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/1 "2021-09-14T04:09:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 14, 2021, 6:04am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/2 "2021-09-14T06:04:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [September 14, 2021, 6:13am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/3 "2021-09-14T06:13:42Z")

</div>

I’m sorry but it’s too long.

---

<div class="post-metadata">

**Author:** ![IlianPihlajamaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilianpihlajamaa/32/28766_2.png) [@IlianPihlajamaa](https://discourse.julialang.org/u/IlianPihlajamaa)\
**Post date:** [September 14, 2021, 6:15am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/4 "2021-09-14T06:15:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 14, 2021, 6:17am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/5 "2021-09-14T06:17:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [September 14, 2021, 6:55am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/6 "2021-09-14T06:55:22Z")

</div>

```julia
# 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)))))

```

---

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [September 14, 2021, 6:59am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/7 "2021-09-14T06:59:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [September 14, 2021, 7:07am UTC](https://discourse.julialang.org/t/why-could-this-happen/68128/8 "2021-09-14T07:07:36Z")

</div>

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:

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

```

instead of

```julia
io = open(...)
println(io, ...)
close(io)

```
