Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Nested List (Please....)

Status
Not open for further replies.

ryy705

Programmer
Sep 29, 2006
1
US
Hi,
I am prolog newbie and I really need some help.
Given a nested list like [[a,1], [b,2], [c,3]], I need to create a new list containing all the letters (ex:[a,b,c]). I am having a very hard time with this. The following is what I have so far.

%main function
%getLettList(+InputList, -ListOfLetters).
getLettList([]|_).
getLettList([H|T],List) :-
putLett(H,List),
getLettList(T,List).


%putLett(+List, -ListOfLetters).
%putLett extracts the first element
%from the List and places it in
%ListOfLetters.
putLett([Lett|Tail], [Lett|List]).

When I compile, I get a warning: "Singleton variables: [Tail,List]"

When I run the prog, with the input getLettList([[a,1],[b,2],[c,3]],[]).
I get no as an output.

Kindly help.
 
%ok
%firsts( [[a,1],[b,2],[c,3]],L).

firsts([[]]):-!.
firsts([[E|_]],[E]).
firsts([[E|_]|R],[E|S]):-firsts(R,S).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top