2

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Joyita
  • 21
  • 2

2 Answers2

1

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

mega_append([],[]).
mega_append([[A|R0]|R1],[A|R2]) :-
        mega_append([R0|R1],R2).
mega_append([[]|R1],R2) :-
        mega_append(R1,R2).
尾崎隆大
  • 148
  • 1
  • 1
  • 1
    No need for three clauses when two suffice. No need for posting your answer twice either; you should have edited the old one. – Fred Foo Apr 27 '11 at 09:02
  • Thank you so much. I've edited the program & It's working; the problem has been solved. – Joyita Apr 27 '11 at 19:58