# Does Julia have an index symbol ! like Haskell?

**URL:** <https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787>\
**Category:** General Usage\
**Created:** [November 6, 2019, 10:21am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787 "2019-11-06T10:21:07Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [November 6, 2019, 10:21am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/1 "2019-11-06T10:21:07Z")

</div>

Hi,

In Haskell you have the !! symbol which is used as follows:

```julia
a = [1,2,3]
a !! 2 

Output:
3 (haskell is 0-based)

```

I find this quite handy but i can only find a regular index method in Julia:

```julia
a[3] = 3

```

Does Julia have this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 6, 2019, 10:24am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/2 "2019-11-06T10:24:25Z")

</div>

why would you type 5 characters when you can just type 3

---

<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:** [November 6, 2019, 10:25am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/3 "2019-11-06T10:25:52Z")

</div>

No, and since `!` is already an operator, you cannot even define an `!!`.

You can alias any other infix operator to `getindex` though:

```julia
julia> const ≃ = getindex
getindex (generic function with 294 methods)

julia> [1, 2, 3] ≃ 2
2

```

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [November 6, 2019, 10:26am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/4 "2019-11-06T10:26:03Z")

</div>

You can define your own operator from that [list](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm#L29) :

```julia
julia> ⊓(x,idx) = x[idx]

⊓ (generic function with 1 method)

julia> [1 2 3] ⊓ 3

3

```

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [November 6, 2019, 10:28am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/5 "2019-11-06T10:28:36Z")

</div>

wow that is powerful and easy!!!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [November 6, 2019, 10:31am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/6 "2019-11-06T10:31:45Z")

</div>

As others have mentioned, you can define your own operators in Julia. My question, however, is why would you think that `a !! 2` is any better than `a[3]`? I think the contrary.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [November 6, 2019, 10:53am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/7 "2019-11-06T10:53:09Z")

</div>

@Seif_Shebl: its the reading flow.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [November 6, 2019, 10:56am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/8 "2019-11-06T10:56:18Z")

</div>

As for symbols, `\rightarrow` and might `\leftarrow` might be a good choice, although they are a bit annoying to type :

```julia
const → = getindex
 ←(x, args) = setindex!(x, args[2], args[1])

julia> [1 2 3] → 3
3

julia>[1 2 3] ← (3,4)
1×3 Array{Int64,2}:
 1 2 4

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [November 6, 2019, 12:10pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/9 "2019-11-06T12:10:12Z")

</div>

While one _can_ do these things, and I for one think it’s fantastic that julia allows this, there’s a different answer to whether or not you _should_. Non-standard solutions to things as simple as indexing make your code less readable / reproducible.

If you have a type defined where there’s a well-thought out and compelling reason use non-standard indexing syntax (as is the case for eg DataFrames), that’s a different matter, but in general I think it’s not a great idea.

---

<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:** [November 6, 2019, 12:59pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/10 "2019-11-06T12:59:44Z")

</div>

I completely agree with you, but at the same time it is natural that some people try to mold a new language they are learning into the image of one they know — it is an interesting experiment and can be a valuable part of the learning process, even if particular modifications are later abandoned in favor of more standard or idiomatic solutions.

---

<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:** [November 6, 2019, 2:37pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/11 "2019-11-06T14:37:46Z")

</div>

> [@AlexanderChen](#):
>
> its the reading flow.

If you look at [this](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)#Syntax) list, you can see that most programming languages use either `[]` or `()` to access list members. Haskell alone uses `a !! 2`.

I’m a bit stumped as to how `a !! 2` has better reading flow, to me it doesn’t look like indexing _at all_. Is it just that you are used to this, or do you think there are some objective advantages?

My guess is, from my _extremely superficial_ knowledge of Haskell, that indexing is not important in Haskell, and therefore they didn’t bother to give it a nice syntax.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 6, 2019, 3:33pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/12 "2019-11-06T15:33:23Z")

</div>

I think Haskell is really cool and I wish I had more opportunities to spend time with it, but my god does it have some awkward (and weird) syntax.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [November 6, 2019, 3:57pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/13 "2019-11-06T15:57:28Z")

</div>

Julia can give you a lot of power, and with great power comes great responsibility

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [November 6, 2019, 4:04pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/14 "2019-11-06T16:04:54Z")

</div>

One thing to consider is that `setindex!` becomes very awkward with this kind of syntax (e.g. in `[1 2 3] ← (3,4)` which is the index ?) but you are not supposed to do much mutation in Haskell as I understand.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [November 6, 2019, 6:40pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/15 "2019-11-06T18:40:00Z")

</div>

@kevbonham: yes i agree. for personal use i see no objection to doing this. If your code needs to be read and used by others i don’t think you should do it.

@Tamas_Papp: I want to gain a deep knowledge of functional programming and that means that I also look into Haskell. Once you get some familiarity with the syntax it has a ‘openness’ that i like, and would like to mimic at least for the time being.

@DNF: yes, I agree it does seem strange at first.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 6, 2019, 6:41pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/16 "2019-11-06T18:41:07Z")

</div>

My Haskell is very rusty, but from what I remember: (1) when `a !! i` is used, `a` is often a list, so the indexing is `O(i)`; (2) collections are often immutable in haskell (new collections are created by mutating elements), so having a operator that is tailored for both access and mutation (as the `[]`) makes less sense. I have always assumed the weird syntax was to remember the user this was not just a `O(1)` direct access which could be as easily transformed in a `O(1)` mutation of that field, it was a completely different creature.

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 6, 2019, 9:12pm UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/17 "2019-11-06T21:12:25Z")

</div>

A point being missed here is that Haskell is lazy and everything is curried. Infix operators ( functions ) are pretty useful in Haskell because of this.

` !! 2` is a function that returns the third element of a collection it’s applied to. `c !!` is a one argument function for indexing into c. Very useful for passing around to HOF.

For this to be useful in Julia you’d want to partial application of an infix operator to the rhs in some nice way.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 7, 2019, 12:12am UTC](https://discourse.julialang.org/t/does-julia-have-an-index-symbol-like-haskell/30787/18 "2019-11-07T00:12:17Z")

</div>

This is a much better reasoning than mine. While a `[]` operator could  
be also used for the same goal using anonymous functions, it would be  
very clumsy compared to the whole rest of the operators that can be  
curried very easily.
