Does Julia have an index symbol ! like Haskell?

Hi,

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

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:

a[3] = 3

Does Julia have this?

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

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

You can alias any other infix operator to getindex though:

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

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

You can define your own operator from that list :

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

⊓ (generic function with 1 method)

julia> [1 2 3] ⊓ 3

3
6 Likes

wow that is powerful and easy!!!

1 Like

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.

@Seif_Shebl: its the reading flow.

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

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

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.

15 Likes

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.

13 Likes

If you look at this 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.

1 Like

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.

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

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.

@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.

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.

4 Likes

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.

8 Likes

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.

1 Like