Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to read a sentence from a file and then copy each of the token one by one into a list

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 5397

Prolog program to read a sentence from a file
and then copy each of the token one by one into a list.

Code for Prolog program to read a sentence from a file and then copy each of the token one by one into a list in Artificial Intelligence

%trace

domains

    file = xinput
    strlist = string*
    
predicates

    start
    readline(strlist)
    createlist(string,strlist,strlist,strlist)
    reverselist(strlist,strlist,strlist)
    
goal

    clearwindow,
    start.
    
clauses

    start:-
        openread(xinput,"data.txt"),
        readdevice(xinput),
        readline([]).

    readline(List):-
        not(eof(xinput)),
        readln(Line),
        createlist(Line,List,Newlist,Act_list),
        readline(Act_list).
    
    readline(List):-
        reverselist(List,[],Reverselist),
        write(Reverselist).

    createlist(Line,Oldlist,Newlist,Act_list):-
        Line <> "",
        fronttoken(Line,Token,Rest),
        Newlist = [Token | Oldlist],
        createlist(Rest,Newlist,List2,Act_list).        

    createlist(Line,Oldlist,Newlist,Act_list):-
        Act_list = Oldlist.

    reverselist([],Inputlist,Inputlist).

    reverselist([Head | Tail],List1,List2):-
        reverselist(Tail,[Head | List1],List2).
        
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to read a sentence from a file and then copy each of the token one by one into a list is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!