Search code examples
prologswi-prolog

Prolog - procedure `A-B' does not exist


I'm new to Prolog and I'm using the SWISH SWI online PROLOG website available here: https://swish.swi-prolog.org/

I'm trying to write a simple program that gets the head and tail of a list via the following query ? - list([H | T]).

However I get the following error: procedure `A-B' does not exist.

This is my list:

list([a, 2,2, b, 3,4,5]).

Theoretically speaking, should I expect 'a' or 'A' as a head result? The tail should be [2, 2, b, 3, 4, 5].


Solution

  • Use Swish like this:

    enter image description here

    But as you can see you have a problem. Your predicate doesn't actually do anything, aside from verifying that what you passed into it is a ./2 structure (what a non-empty prolog list is).

    To get what you want, try something like this — https://swish.swi-prolog.org/p/DbwbcXxp.pl

    pop( [H|T] , H , T ) .
    

    Now when you execute the query pop( [a,b,c,d,e,f], H , T ), you get

    H = a
    T = [b,c,d,e,f]
    

    But you don't actually need a predicate to decompose a list. Assuming you have a non-empty list L, you can decompose it into its head and tail using the unification operator (=/2): Executing this query,

    L  = [a,b,c,d,e,f], [H|T] = L .
    

    yields

    H = a
    T = [b,c,d,e,f]
    

    as above.