0

In this first picture I get the first element of the array by using:

subscript(array,[1],First)

returns_one

In this second picture I try the same but then for the second element but it gets everything from the second element to the end of the array. I just want the second element not the rest.

returns_rest

How does subscript exactly work?

Stanko
  • 4,275
  • 3
  • 23
  • 51
  • 2
    I don't know much about ECLiPSe, but it looks like this boils down to what kind of term structure it sees for `[1, 9, ...]`. The fact that `[2]` yields the tail of the list implies to me that the structure is something like Prolog's list term, `'.'(H, T)` which is what is represented by the syntax `[H|T]` in Prolog. So `[2]` (which gives the second argument for the term) is yielding `T`. Just a thought... – lurker May 18 '16 at 12:24

1 Answers1

3

As @lurker says in his comment, you are trying to apply subscript/3 to a list, but it expects an array or any other flat structure.

By convention, ECLiPSe uses structures with the functor '[]'/N for arrays. You can create them either by writing them literally

Array = [](5,Y,Z,9,2)

or by creating them with dim/2

dim(Array, [5])

or by converting them from a list

array_list(Array, [5,Y,Z,9,2])

On such arrays, subscript/3 works as expected:

?- Array = [](5,X,Z,9,2), subscript(Array,[4],Elem).
Elem = 9
Yes (0.00s cpu)

Note that subscript/3 is implicitly called when you use array notation inside an arithmetic expression:

?- Array = [](5,X,Z,9,2), Result is Array[4] + 1.
Result = 10
Yes (0.00s cpu)
jschimpf
  • 4,904
  • 11
  • 24