FLP LFunction and Logic Programming
FLP LFunction and Logic Programming
NCS-455
[1] Write a function for the binary operations like addition, subtraction, multiplication and
division in LISP
Theory: Lisp uses prefix notation for computation. For Example: (5+3+4)
Addition:
(+ 2 3)
5
(+ 5 3 4)
12
Subtraction:
(- 10 8)
2
(- 2 10)
-8
(- 10 8 6)
-4
Multiplication:
(* 5 5)
25
(* 2 2 2)
8
Division:
(/ 5 5)
1
(/ 10 2 5)
1
(/ 10 2 4)
5/4
(/ 2 5 5)
2/25
[2] Write a function that computes the factorial of a number in LISP(Factorial for 0 is 1,
and factorial for n is n*(n-1)*1)
Code:
(
defun factorial (number)
(if (= number 0) 1
(* number (factorial (- number 1)))
)
Output:
(Factorial 3)
6
(Factorial 0)
1
Code:
(
defun avg3(a b c)
(princ the average of 3 nos is : )
(/ (+ a b c) 3)
)
Output:
(Avg3 1 2 3)
The average of 3 nos is : 2
[6] Write a function that evaluates a fully parenthesized infix arithmetic expression in
LISP
Code: (20 * 5 / 5 ) + 10
(+ (* (/ 5 5) 20) 10)
Output:
30
(1+ (2*3))
(+ (* 2 3) 1) =7
[7] Write a function that performs a depth first traversal of binary tree.
Theory: An exhaustive search method. DFS is a search algorithm that follows each path to its
greatest depth before moving on to the next path.
(defun dfs (state depth limit)
(setf *nodes* 0)
(setf *expanded* 0)
(setf *branches* 0)
(setf *limit* limit)
(setf *result* (dfs1 state depth))
(print (list *nodes* *expanded* *branches*))
*result*
)
;;; dfs1 expands a node and calls dfs2 to recurse on it
(defun dfs1 (state depth)
(setf *nodes* (+ 1 *nodes*))
(cond
((goalp state) (list state))
((zerop depth) nil)
((> *nodes* *limit*) nil)
((let ((children (new-states state)))
(setf *expanded* (+ 1 *expanded*))
(setf *branches* (+ (length children) *branches*))
(let ((result (dfs2 children (- depth 1))))
(and result (cons state result)))))))
;;; dfs2 recurses on each sibling from a single node, calling dfs1
(defun dfs2 (states depth)
(cond
((null states) nil)
((dfs1 (car states) depth))
((dfs2 (cdr states) depth))))
10
11
12
[9] Write a PROLOG program that answers questions about family members and
relationships includes predicates and rules which define sister, brother, father, mother,
grandchild, grandfather and uncle. The program should be able to answer queries such as
the following:
Father(x,Amit)
Grandson(x,y)
Uncle(sumit,puneet)
Mother(anita,x)
Domains
List = string*
Predicates
Father(string, string)
Grandson(string, string)
Uncle(string, string)
Mother(string, string)
Clauses
Father(Suraj,Amit) //father of suraj is amit
Father(Rohit,Raj)
Grandson (Suraj,Suresh)
Father(Rohit,Raj)
Mother(anita,suraj)
Mother(anita,vimal)
Uncle(sumit,puneet)
Uncle(rahul,puneet)
Grandson(suresh,amit)
Goal:
Father(x,Amit)
x= Suraj
1 Solution
13
Predicates
insert(integer, list, list).
append(list, list, list).
insort(list, list).
Clauses
append([],L,L).
append([H|T],L,[H|NT]):-append(T,L,NT).
insert(X,[],[X]).
insert(X,[H|T],NL):-X<H, append([X],[H|T],NL).
insert(X,[H|T],[H|NL]):-X>H, insert(X,T,NL).
insort([], []).
insort([H|T],FS):-insort(T,NT), insert(H,NT,FS).
14
Partition(A,p,r)
x <- A[p]
i <- p-1
j <- r+1
while (True) {
repeat
j <- j-1
until (A[j] <= x)
repeat
i <- i+1
until (A[i] >= x)
if (i<-=""> A[j]
else
return(j)
}
}
Code in LISP:
Domains
list = integer*.
Predicates
quicksort(list,list).
split(integer,list,list,list).
concatenate(list,list,list).
printlist(list).
Clauses
quicksort([],[]).
quicksort([Head|Tail],SortedList) :split(Head,Tail,SList,BList),
quicksort(SList,SList1),
quicksort(BList,BList1),
concatenate(SList1,[Head|Blist1],SortedList),
printlist(SortedList).
split(_,[],[],[]).
split(Item,[Head1|Tail1],[Head1|SList],BList) :15
Output
Goal: quicksort([2,4,1,3,5,9,6],L).
1
3
6
69
569
34569
1234569
L=[1,2,3,4,5,6,9]
1 Solution
16
Output:
17
Goal: bubblesort([2,3,1,4],L).
2134
1234
L=[1,2,3,4]
1 Solution
Goal: bubblesort([2,4,1,3,5,9,6],L).
2143596
1243596
1234596
1234569
L= [1,2,3,4,5,6,9]
1 Solution
18
19