In python if I want to retrieve the second element of a list, I can do it like this:
>>> A = [1, 2, 3]
>>> A[1]
2
What is the equivalent of it in erlang?
So either use lists:append/1 after your examples:reverse/1 function, or replace reverse([H | T]) -> [reverse(T) | [H]]; with reverse([H | T]) -> reverse(T) ++ [H]; .
Index() method is required to get the position of an item in the list.
You can't seriously program in a language just with scalar types like numbers, strings and atoms. For this reason, now that we have a basic knowledge of Erlang's syntax and variables, we have to delve into two basic vector types: tuples and lists.
In Erlang, Lists are created by enclosing the values in square brackets.
We can use the nth
method in the lists
module.
1> A = [1, 2, 3].
[1,2,3]
2> lists:nth(2, A).
2
Note: The index is not zero based.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With