Problem in appending nested list in turbo prolog

1.2k Views Asked by At

I'm new in turbo prolog.I'm facing a problem in appending nested list. I want the goal like this-

Goal: mega_append([[1,3],[2,4,6],[0]],X)

Output should be X=[1,3,2,4,6,0]

I used the following codes:

domains 

list=integer*

predicates

    mega_append(list,list)
    append(list,list,list)

clauses

    mega_append([],[]).

    mega_append([H|T],L3):-
        mega_append(H,L1),
        mega_append(T,L2),
        append(L1,L2,L3).

    mega_append(X,[X]).

    append([],ListB,ListB).

    append([X|List1],List2,[X|List3]):-
        append(List1,List2,List3).

The program generates an error.It is "illegal variable type in this position." Then it indicates the position of H in the line- mega_append(H,L1). How can I get rid of this problem? Is there any mistake in my program? Please help me.

2

There are 2 best solutions below

2
On

You're manipulating lists of lists, not lists of integers. Try

listlist = list*
mega_append(listlist,list)

Then, fix your recursion. The version you wrote won't work in a statically typed variant of Prolog, which Turbo Prolog apparently is: mega_append is called recursively on the head of its first argument, which is of a different type then the argument itself (if it is of type T*, then its head must be of type T).

mega_append([], []).
mega_append([H|T], R) :-
    mega_append(T, R0),
    append(H, R0, R).

And btw., the common name for this predicate is concat/2.

2
On

mega_append([],[]).
mega_append([[A|R0]|R1],[A|R2]) :-
        mega_append([R0|R1],R2).
mega_append([[]|R1],R2) :-
        mega_append(R1,R2).