Simple Programs for Prolog

  • Maximum value from the list:

    domains
    numlist=integer*

    predicates
    max(numlist,integer)
    maxim(numlist)
    clauses
    maxim(L):-
    max(L,0).

    max([],Maxim):-
    write(“\nmaximum=”),write(Maxim).

    max([H|T],Maxim):-
    Maxim=H,max(T,Maxim).

  • Minimum value from the list:

    domains
    numlist=integer*

    predicates
    max(numlist,integer)
    maxim(numlist)
    clauses
    maxim(L):-
    max(L,32767).

    max([],Maxim):-
    write(“\nmaximum=”),write(Maxim).

    max([H|T],Maxim):-
    MaximH,max(T,H).

  • Find average value from the list:

    domains
    int = integer*
    predicates
    avg(int, integer, integer)
    clauses

    avg([],A,T):-
    Z = A/T,
    write(Z).
    avg([Head|Tail], A, T):-
    Z = A + Head,
    TT = T + 1,
    avg(Tail,Z,TT).

  • Write a program for user to LOGON ROUTINE on for highest 3 times. :

    predicates
    getinput(symbol,symbol)
    logon(integer)
    logon
    user(symbol,symbol)

    clauses
    logon:-
    logon(0).
    logon(3):-
    write(“No Attempt Left, Login Failed”),nl.
    logon(N):-
    getinput(Name,Password),
    user(Name,Password),
    write(“You logged on as “,Name,”.”),nl,N=N.
    logon(N):-
    write(“Invalid Login! Retry”),nl,
    NN=N+1,logon(NN).

    getinput(Name,Password):-
    write(“Name: “),readln(Name),
    write(“Password: “),readln(Password).

    user(“John”,”superman”).
    user(“Bob”,”1234″).

  • Grade finding out where input is : Roll, subject, credit

    domains
    location=address(subC1,subC2,subC3,grad1,grad2,grad3)
    subC1,subC2,subC3,grad1,grad2,grad3=real.
    predicates
    person(string,string,location)
    go
    clauses
    go:-
    write(“enter name & roll”),
    readln(Name),
    readln(Roll),
    person(Name,Roll,address(X1,X2,X3,X4,X5,X6)),
    Cgpa = (X1*X4+X2*X5+X3*X6)/(X1+X2+X3),
    write(Name),
    write(“‘s cgpa is :”),
    write(Cgpa).

    person(“rana”,”0507056″,address(3,2.5,4,3,3.5,4)).
    person(“ovi”,”0507055″,address(4,3,2,3,4,3.5)).
    person(“faisal”,”0507006″,address(3,3,4,3.5,3,4)).

  • Leave a Reply

    Please log in using one of these methods to post your comment:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s