简体   繁体   中英

Trying to learn Prolog, can't figure out what's wrong

Doing a small example, I want to insert a symbol within a list at a certain pozition

domains
    element = symbol
    list = element*
    position = integer
predicates
    insert (element, position, list, list) %(input,input,input,output)
clauses 
    insert (E,_,[],[E]). 
    insert (E, 1,[H|T],[E|[H|T]]).       %(I insert E on the "first" position)
    % I get errors how E and T are not bound. I have no idea how to make them bound
    insert (E,P,[H|T],[H1|T1]) :-
           P > 1,
           P1 = P - 1,
           insert (E,P1,T,T1).

It doesn't work ... but I don't know why. Well it kinda works. I would like it to show me outputList = [NEW_LIST] instead of showing every symbolName=_ and then outputList = [_,_,_,_] .

Apparently there is some problem with symbol data type, if I use integer, it fixes itself.

Also, this is esential:

domains
    element = integer
     list = element*
     position = integer
predicates
     insert (element, position, list, list) %(input,input,input,output)
clauses 
     insert (E,_,[],[E]). 
     insert (E, 1,[H|T],[E|[H|T]]).
     insert (E,P,[H|T],[H|T1]) :-  % has to be "H" (or anything else) in both
          P > 1,                   % so prolog understands what we are trying to do
          P1 = P - 1,              % don't really understand why though
          insert (E,P1,T,T1).      % I might be wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM