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: 4155

Write a complete 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

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

    reading(List):-
        not(eof(xinput)),
        readln(Line),
        createlist(Line,List,Newlist,Act_list),
        reading(Act_list).
    
    reading(List):-
        reverse(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.

    reverse([],Inputlist,Inputlist).

    reverse([Head | Tail],List1,List2):-
        reverse(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:


 

Other Interesting Articles in Artificial Intelligence:


 
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!