Search code examples
prolog

Prolog - Finding specific elements index


helllo iam new to prolog and iam trying to make a function returns a specific elements index for example indexof (4,[3,4,5],X) X is 1 any help is really appreciated


Solution

  • Use nth0/3

    ?- nth0(X,[3,4,5],4).
    X = 1 ;
    false.
    

    If you go to the documentation page and click on enter image description here it will take you to the Prolog source code that implements nth0/3. (link)