Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to read file and create list of all the lines of it. Each list in turn should be list of word.

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

Write a complete prolog program to read file and create list
of all the lines of it. Each list in turn should be list of
word.

Code for Prolog program to read file and create list of all the lines of it. Each list in turn should be list of word. in Artificial Intelligence

domains
    file = xinput
    strlist = string*
    mul_strlist = strlist*
predicates
    start
    reading(mul_strlist)
    createlist(string,strlist,strlist,mul_strlist,mul_strlist)
    reverselist(strlist,strlist,strlist)
goal
    clearwindow,
    start.
clauses
    start:-    
        openread(xinput,"data.txt"),
        readdevice(xinput),
        reading([]).

    reading(Mlist):-
        not(eof(xinput)),
        readln(Line),
        createlist(Line,[],New_list,MList,List2),
        reading(List2).
        
    reading(Mlist):-
        write(Mlist),
        closefile(xinput).

    createlist(Line,Old_list,New_list,List1,List2):-
        Line <> "",
        fronttoken(Line,Token,Rest),
        New_list = [Token | Old_list],
        createlist(Rest,New_list,Blank_list,List1,List2).

    createlist(_,Old_list,[],List1,List2):-
        reverselist(Old_list,[],New_list),
        List2 = [New_list | List1].        

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



Milind Mishra
Milind Mishra author of Prolog program to read file and create list of all the lines of it. Each list in turn should be list of word. 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!