flp lfunction and logic programming

Upload: kanika-garg

Post on 09-Mar-2016

75 views

Category:

Documents


2 download

TRANSCRIPT

Lab ManualFunctional & Logic Programming

NCS-455

FUNCTIONAL AND LOGIC PROGRAMMING LAB

(NCS-455)LABORATORY MANUALFORBachelor of TechnologyInComputer ScienceSession: 2014-2015

Department of Computer Science & EngineeringKRISHNA ENGINEERING COLLEGEGHAZIABADNCS-455: FUNCTIONAL AND LOGIC PROGRAMMING LAB1. Write a function for the binary operations like addition, subtraction, multiplication and division in LISP(3)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).............(4)3. Write a function that computes average of three numbers in LISP......(5)4. Write a function that computes area of a circle in LISP....(6)5. Write a function that computes maximum of three numbers in LISP..(7)6. Write a function that evaluates a fully parenthesized infix arithmetic expression in LISP.............(8)7. Write a function that performs a depth first traversal of binary tree...(9)8. Write a LISP program for water jug problem..(10)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:..(12) Father(x,Amit)

Grandson(x,y)

Uncle(sumit,puneet)

Mother(anita,x) Program in SML-NJ or CAML or PROLOG for following:

10. To implement Insertion Sorting(13)11. To implement Quick Sorting.(14)12. To implement Bubble Sorting...(16)13. To implement Selection Sorting(17)LISP is the premier language for Artificial Intelligence applications. It is adynamic language:editing changes take effect immediately, without the need for recompilation. It is primarily afunctional language: all work can be done via function composition and recursion. There is no "main program:'' the programmer can call any function from the input prompt.

The overall style of the language is organized primarily around expressions and functions rather than statements and subroutines.

LISP is an old language, and has been updated many times. The most recent standard,Common LISP,is a huge language.

Here is a simple Lisp procedure that concatenates two lists of items, producing a new list:

(define (append x y)

(cond ( (null x) y)

(t (cons (car x) (append (cdr x) y)))))

This may be read as follows: To produce a list consisting of the items of y appended to the items of x, the computation is conditional (cond). If the list x is empty (null), then the result is equal to y. Otherwise, construct (cons) a new list by placing the first item (car) of x before the result of appending y to the rest of the items (cdr) of list x.

A list is a sequence of Lisp data objects, notated by notating its elements within parentheses and separated by spaces. Thus (michelangelo artist (born 1475) (died 1564))is a list of four items; the first two are symbols, and the last two are lists, each containing a symbol and an integer. The list () is empty, containing zero items; (() ()) is a list of two empty lists.

We can apply our sample function append to two lists as follows:

(append '(fee fie) '(foe fum)) =>; (FEE FIE FOE FUM)

[1] Write a function for the binary operations like addition, subtraction, multiplication and division in LISPTheory: 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

[3] Write a function that computes average of three numbers in LISP.

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 [4] Write a function that computes area of a circle in LISP

Code:

(

defun circle()

(princ enter the radius : )

(setq r (read))

(princ the area of circle is :)

(* 3.14 r r)

)

Output:

(circle)

Enter the radius :

2

The area of circle is : 12.56

[5] Write a function that computes maximum of three numbers in LISP

Code:

(

defun max3(a b c)

(cond((> a b)(cond((> a c) a)

(t c)))

((> b c) b)

(t c ))

)

Output:

(max3 7 1 4)

7

[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))))

[8] Write a LISP program for water jug problem.

Theory: we have a Four Gallon Jug of Water and a Three Gallon Jug of Water and a Water Pump. The challenge of the problem is to be able to put exactly two gallons of water in the Four Gallon Jug, even though there are no markings on the Jugs (defvar *start* '(0 0))

(defun first-jug (state) (car state))

(defun second-jug (state) (cadr state))

(defun mk-state (f s) (list f s))

(defun goalp (state)

(eq (first-jug state) 2))

(defun new-states (state)

(remove-null

(list

(fill-first state)

(fill-second state)

(pour-first-second state)

(pour-second-first state)

(empty-first state)

(empty-second state))))

(defun remove-null (x)

(cond

((null x) nil)

((null (car x)) (remove-null (cdr x)))

((cons (car x) (remove-null (cdr x))))))

(defun fill-first (state)

(cond

((< (first-jug state) 4) (mk-state 4 (second-jug state))))))

(defun fill-second (state)

(cond

((< (second-jug state) 3) (mk-state (first-jug state) 3))))

(defun pour-first-second (state)

(let ( (f (first-jug state))

(s (second-jug state)))

(cond

((zerop f) nil)

; Cant pour nothing

((= s 3) nil)

; Second full

(( (second-jug state) 0) (mk-state (first-jug state) 0))))

[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

[10] Implement Insertion sort using PROLOG.

Theory: Insertion Sortis an efficientalgorithmfor sorting a small number of elements.

INSERTION-SORT (A)1. for j = 2 to n2. key A [j]3. // Insert A[j] into the sorted sequence A[1..j-1]4. j i 15. while i > 0 and A[i] > key6. A[i+1] A[i]7. i i 18. A[j+1] key

Code in LISP: Domains

list = integer*.

Predicates

insert(integer, list, list).

append(list, list, list).

insort(list, list).

Clausesappend([],L,L).

append([H|T],L,[H|NT]):-append(T,L,NT).

insert(X,[],[X]).

insert(X,[H|T],NL):-XH, insert(X,T,NL).

insort([], []).

insort([H|T],FS):-insort(T,NT), insert(H,NT,FS).

[11] Implement Quick Sort using PROLOGPseudo Code:Quicksort(A,p,r) {

if (p < r) {

q