0% found this document useful (1 vote)
253 views

Functional and Logic Programming

The document contains 12 programs written in various programming languages like C, Lisp, Prolog. The programs cover concepts like linear search, binary search, bubble sort, quick sort, factorial calculation, temperature conversion, property insertion, depth-first search, breadth-first search, water jug problem and towers of Hanoi problem. The programs are part of a lab assignment submitted by a student for the subject of Functional and Logic Programming.

Uploaded by

Nikhil K Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
253 views

Functional and Logic Programming

The document contains 12 programs written in various programming languages like C, Lisp, Prolog. The programs cover concepts like linear search, binary search, bubble sort, quick sort, factorial calculation, temperature conversion, property insertion, depth-first search, breadth-first search, water jug problem and towers of Hanoi problem. The programs are part of a lab assignment submitted by a student for the subject of Functional and Logic Programming.

Uploaded by

Nikhil K Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SKYLINE INSTITUTE OF ENGINEERING & TECHNOLOGY,

Greater Noida

DEPARTMENT OF COMPUTER SCIENCE AND ENGG.

SESSION : 2014-15

Functional And Logic Programming Lab (NCS-455)

SUBMITTED TO :
Mrs Anuradha

SUBMITTED BY:

INDEX
S.No.

01

02

03

04

05

06

07

08

09

10

11
12

NAME OF PROGRAM

DATE

PAGE No.

SIGN.

Program-1

Program in c for linear search

#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search)

/* if required element found */

{
printf("%d is present at location %d.\n", search, c+1);
break;
}

}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

Program-2

Program in c for binary search

#include<stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;

else if (array[middle] == search) {


printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);

return 0;
}

Program-3

Program in c for bubble sort

#include<stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap

= array[d];

array[d] = array[d+1];
array[d+1] = swap;
}

}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}

Program-4

Program in c for quick sort

#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");


for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

Program-5

Program in Lisp to find the factorial of a number

(defun factorial (n)


(if (= n 1)
1
(* n (factorial (- n 1)) )
)
)

Program-6

Program in Lisp to convert the Fahrenheit degree to the equivalent Celsius degree

(defun convert ()
(format t "Enter Fahrenheit ")
(LET (fahr)
(SETQ fahr (read fahr))
(APPEND '(celsisus is) (*(- fahr 32)(/ 5 9)) )
)
)

Program-7

Program in LISP for inserting new properties in the old properties of the any existing
function

(putprop danny(tommy killer tiger) pets)


(TOMMY KILLER TIGER)
(putprop danny(cons sweto(get danny pets)) pets)

Program-8

Program in LISP for Depth First Search and Breadth First Searches

(defun binary-tree (x)


"The Data Set, the actual tree"
(list (* 2 x) (+ 1 (* 2 x))))
(defun finite-binary-tree(n)
"This Function was given in class
It Limits the tree to the value passed n"
#'(lambda(x)
(remove-if #'(lambda (child) (> child n))
(binary-tree x))))
(defun tree-search (states goal-p successors combiner)
"Search algorythem dependant on parameters"
(print states)
(cond ((null states) fail)
((funcall goal-p (car states)) (car states))
(t (tree-search
(funcall combiner
(funcall successors (car states))
(cdr states))
goal-p successors combiner))))
(defun depth-first-search (start goal-p successors)
"newest nodes expanded until goal is reached and
reverts when cutoff is hit - 128"
(tree-search (list start) goal-p successors #'append))

(defun breadth-first-search (start goal-p successors)


"Oldest node searched first and expanded nodes are added to the end of the
search list. Finding node 4 will take 4 turns and finding 66 will take 66"
(tree-search (list start) goal-p successors #'swapsuccessors))
(defun swapsuccessors(x y)
"moves the child nodes to the end of the list"
(append y x))
(defun is (value)
#'(lambda (x) (eql x value)))

Program-9

Program in Lisp for the Water-Jug problem

;;; Solve the Water Jug problem


(in-package "USER")
(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

((<= (+ f s) 3)

; Empty first into second

(mk-state 0 (+ f s)))
(t

; Fill second from first


(mk-state (- (+ f s) 3) 3)))))

(defun pour-second-first (state)


(let ( (f (first-jug state))
(s (second-jug state)))
(cond
((zerop s) nil)

; Cant pour nothing

((= f 4) nil)

; First full

((<= (+ f s) 4)

; Empty second into first

(mk-state (+ f s) 0))
(t

; Fill first from second


(mk-state 4 (- (+ f s) 4))))))

(defun empty-first (state)


(cond

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

(defun empty-second (state)


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

Program-10

PROLOG program for adding the simple facts in its database


% List of parent relationships
% 'facts.prolog'
parent(kim,holly).
parent(margaret,kim
parent(herbert,margaret).
parent(john,kim).
parent(felix,john).
parent(albert,felix).
?- consult('facts.prolog').

Program-11

Query process in PROLOG


% 'facts.prolog'
parent(kim,holly).
parent(margaret,kim
parent(herbert,margaret).
parent(john,kim).
parent(felix,john).
parent(albert,felix).
?- consult('facts.prolog').
?- parent(margaret, john).
No
?- parent(margaret,kim).
Yes

Program-12

Program in Prolog for Towers of Hanoi problem

move(1,X,Y,_) :write('Move top disk from '),


write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

You might also like