How can I get nth element from a list?

eonil picture eonil · Mar 7, 2011 · Viewed 159.5k times · Source

How can I access a list by index in Haskell, analog to this C code?

int a[] = { 34, 45, 56 };
return a[1];

Answer

phimuemue picture phimuemue · Mar 7, 2011

Look here, the operator used is !!.

I.e. [1,2,3]!!1 gives you 2, since lists are 0-indexed.