# Simpler identifier for last result than ans

**URL:** https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568
**Category:** General Usage
**Tags:** question
**Created:** [June 23, 2019, 10:26am UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568 "2019-06-23T10:26:54Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 10:26am UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/1 "2019-06-23T10:26:54Z")

</div>

Hello,  
With ans I can refer to the last result in Julia.  
In ipython you can do the same with \_ (just an underscore).

Is there a way to use a different identifier than ans to refer  
to the last result? Preferably using only one letter or symbol?

Uwe

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 23, 2019, 1:23pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/2 "2019-06-23T13:23:06Z")

</div>

There is not. It is set here:

[https://github.com/JuliaLang/julia/blob/f6049d6d25d720bc90d82aadae6be98bc948ef24/stdlib/REPL/src/REPL.jl#L89](https://github.com/JuliaLang/julia/blob/f6049d6d25d720bc90d82aadae6be98bc948ef24/stdlib/REPL/src/REPL.jl#L89)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 23, 2019, 1:34pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/3 "2019-06-23T13:34:16Z")

</div>

I am curious why you need this. In the last year, I have use `ans` less than 10 times. Usually when I forget to assign

```julia
some_complicated_calculation_that_takes_10_minutes(input)

```

to something, I do an

```julia
result_that_justified_a_coffee_break = ans

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 1:40pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/4 "2019-06-23T13:40:12Z")

</div>

In the moment I am using ipython as kind of scientific calculator. Every day I do a lot of small calculations with it, and using \_ for the last result is very practical for me. I would prefer to use julia as calculator, for example because it has functions like sind and cosd which come in handy, and also to stick to one language.

So perhaps I can hack the source code to get what I want?

But there should be a better way to make the REPL more adaptable.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 23, 2019, 1:59pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/5 "2019-06-23T13:59:53Z")

</div>

> [@ufechner7](#):
>
> But there should be a better way to make the REPL more adaptable.

I would just use an IDE where you write the code in a window and send it to a REPL in a simple manner (a couple of keystrokes at most). This should be no additional effort compared to a standard workflow, and should be a lot more organized and transparent.

I am very skeptical of the “calculator” workflow even for simple calculations. I think that assigning a single-letter variable is not a great cost. YMMV.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 2:33pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/6 "2019-06-23T14:33:57Z")

</div>

Well, an IDE is very heavy weight, compared to a REPL. IPython is very adaptable, using a configuration file. This should also be possible with the Julia REPL. We want to be superior to Python, or not?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 23, 2019, 2:52pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/7 "2019-06-23T14:52:02Z")

</div>

> [@ufechner7](#):
>
> Well, an IDE is very heavy weight, compared to a REPL.

I don’t know what the concern is here, startup time? I don’t know other IDEs, but an Emacs set up for Julia can start up in about 1s on my computer.

> [@ufechner7](#):
>
> We want to be superior to Python, or not?

I don’t quite understand what it means in this context. For me, this _per se_ would not justify porting features directly from Python. In some cases, not doing something can be understood as an improvement.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 3:26pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/8 "2019-06-23T15:26:55Z")

</div>

> [@Tamas\_Papp](#):
>
> I don’t know what the concern is here, startup time? I don’t know other IDEs, but an Emacs set up for Julia can start up in about 1s on my computer.

Its not so much about the startup time, but about the screen usage. A scientific calculator is used aside of a text document, web site or spread sheet. I do not know emacs, I only know vscode and atom. And they need the whole screen.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 3:28pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/9 "2019-06-23T15:28:10Z")

</div>

> [@Tamas\_Papp](#):
>
> I don’t quite understand what it means in this context. For me, this _per se_ would not justify porting features directly from Python. In some cases, not doing something can be understood as an improvement.

I am talking about REPL features, this means I compare IPython with the Julia REPL. Adding more features from IPython to the Julia REPL would be very valuable.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 23, 2019, 5:10pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/10 "2019-06-23T17:10:28Z")

</div>

AFAICT, you just want to use as close a syntax you use in ipython as possible, this is in general never going to happen.

For customizing the REPL, I believe this is certainly doable using a OhMyREPL level of hack though I don’t think you should do it.

Making the REPL customizable is nice, but that doesn’t necessarily include this feature since what you are asking for is much more than customizing the display. You are asking for changing of the meaning of valid code, even if it’s only for the REPL. That comes at the cost of not being able to understand/use other’s code. In another word, it will only not cause any problem if you never copy and paste your REPL anywhere and you never paste anything into your REPL, otherwise, you are bound to get something unexpected sooner or later.

> [@ufechner7](#):
>
> Adding more features from IPython to the Julia REPL would be very valuable.

Yes, but that doesn’t mean changing the julia (REPL) syntax to match python’s.

Edit: Also, as for `_` and `ans`. I don’t think `_` is too much better. At least on a normal (english) keyboard, `_` requires pressing down two keys simultaneously (and typing it on a phone is even harder) while `ans` takes three key sequantial presses which is a wash IMO…

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 5:55pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/11 "2019-06-23T17:55:55Z")

</div>

I do not agree. The point is not to mimic the behaviour of IPython as close as possible, but to make to possible to adapt to the easy of use of IPython in contrast to the complicated way Matlab handles this issue.  
I do not understand why you insist that the complicated approach of Matlab (using ans) is the only valid way for Julia.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 23, 2019, 5:58pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/12 "2019-06-23T17:58:15Z")

</div>

To be more precise:  
\_ gives you the last result in IPython  
\_\_\_ gives you the last-last result in IPython

How can you access this value in Julia?

This is just a feature request. And you say we shall never add features to the Julia REPL?

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [June 23, 2019, 6:17pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/13 "2019-06-23T18:17:17Z")

</div>

I think it would only require 5 - 10 short lines of code to do this.  
Here’s a start

```julia
while true
   print("> ")
   println(global ans = eval(Meta.parse(readline())))
end

```

Instead of `ans`, you can use any legal variable name. (A slight complication is that all-underscore variable names are not allowed in Julia, so one would have to do some substitutions in the parsed expression to get the exact IPython syntax.)

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 23, 2019, 6:32pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/14 "2019-06-23T18:32:24Z")

</div>

> [@ufechner7](#):
>
> I do not understand why you insist that the complicated approach of Matlab (using ans) is the only valid way for Julia.

1. I do not agree `ans` is more complicated, not by much anyway, as explained above. I have also had one reply on this so I’m hardly insisting. I also give the reason for all my statements so if you don’t understand please quote which reasoning you don’t understand.
2. I did not say `ans` is the only valid way, only it is not something that should be “adaptive”, which I interpreted as configurable/customizable. In another word, as I said above already, it should not be something that you should configure yourself, the reason for which is also included above.

> [@ufechner7](#):
>
> \_ gives you the last result in IPython

Correct (even though I didn’t say anything about this above), this should not be added since it conflicts with possible other uses of the syntax that have not been finalized. Other than that, changing to this isn’t as much of a big deal as long as everyone get the change. It’ll be breaking and as I said, I personally don’t think it’s better than `ans`.

> [@ufechner7](#):
>
> \_\_\_ gives you the last-last result in IPython

This is a completely different feature that you didn’t even mentioned before so I have never said anything about adding such a feature.

Such feature, or even the more general `In`/`Out` array could be added, with the caveat of memory consumption for the array version. Still, it affects code execution on **valid** syntax so it’s not something that you should easily configure on your own. If you mean “adaptive” as in changing the REPL to implement this it is certainly something that’s easy to do, picking the name will likely be the hard part.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [June 23, 2019, 8:07pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/16 "2019-06-23T20:07:57Z")

</div>

This will let you use a symbol instead of `ans`:

```julia
julia> using ReplMaker, MacroTools

julia> last_key(ex) = MacroTools.postwalk(x -> x==:% ? :ans : x, Meta.parse(ex));

julia> initrepl(last_key; prompt_text="Julia> ", prompt_color=:red, start_key='%', mode_name="last-key")
REPL mode last-key initialized. Press % to enter and backspace to exit.

Julia> 1+2
3

Julia> [i^2 for i=1:%]
3-element Array{Int64,1}:
 1
 4
 9

```

Whether you can easily make a repl mode which saves older output I don’t know, certainly I can’t in 5 minutes. But if you find a way, others may find that useful.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 23, 2019, 9:08pm UTC](https://discourse.julialang.org/t/simpler-identifier-for-last-result-than-ans/25568/17 "2019-06-23T21:08:12Z")

</div>

Would it be possible or advisable to make some non-valid identifier contain the last, or next-to-last etc. value, for use in the REPL? Such as `1_`, `2_`, etc. or something starting with `#`? I’m just thinking a bit freely, grasping for something that would not change the meaning of valid code.
