0% found this document useful (0 votes)
15 views

CPP Practicalsfinal

Cop

Uploaded by

higkiller8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CPP Practicalsfinal

Cop

Uploaded by

higkiller8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CPP PRACTICALS

1. Create a RECORD IN HASKELL


Program:
data Person = Person { firstName :: String, lastName :: String, degree :: String, college ::
String, year :: String } deriving Show

person1 = Person "Janaki" "Koli" "IT" "RIZVI" "2"


person2 = Person "Yashraj" "Pawar" "IT" "XAVIER INSTITUTE" "2"
person3 = Person "Jayesh" "Rode" "IT" "K.J.SOMAIYA" "2"
person4 = Person "Vaishali" "Kale" "IT" "SHAH & ANCHOR" "2"

main = do

print person1
print $ firstName person1
print $ lastName person1
print $ degree person1
print $ college person1
print $ year person1

print person2
print $ firstName person2
print $ lastName person2
print $ degree person2
print $ college person2
print $ year person2

print person3
print $ firstName person3
print $ lastName person3
print $ degree person3
print $ college person3
print $ year person3

print person4
print $ firstName person4
print $ lastName person4
print $ degree person4
print $ college person4
print $ year person4

Output:
[?2004l
Person {firstName = "Janaki", lastName = "Koli", degree = "IT", college = "RIZVI", year = "2"}
"Janaki"
"Koli"
"IT"
"RIZVI"
"2"
Person {firstName = "Yashraj", lastName = "Pawar", degree = "IT", college = "XAVIER
INSTITUTE", year = "2"}
"Yashraj"
"Pawar"
"IT"
"XAVIER INSTITUTE"
"2"
Person {firstName = "Jayesh", lastName = "Rode", degree = "IT", college = "K.J.SOMAIYA",
year = "2"}
"Jayesh"
"Rode"
"IT"
"K.J.SOMAIYA"
"2"
Person {firstName = "Vaishali", lastName = "Kale", degree = "IT", college = "SHAH &
ANCHOR", year = "2"}
"Vaishali"
"Kale"
"IT"
"SHAH & ANCHOR"
"2"
[?2004h

2. Perform CALENDER GENERATION IN HASKELL.


Program:
data DayOfWeek
= Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday
deriving (Eq, Enum, Bounded)

data Month
= January | February | March | April | May | June
| July | August | September | October | November | December
deriving (Enum, Bounded, Show)

next :: (Eq a, Enum a, Bounded a) => a -> a


next x | x == maxBound = minBound
| otherwise = succ x

pad :: Int -> String


pad day = case show day of
[c] -> [' ', c]
cs -> cs

month :: Month -> DayOfWeek -> Int -> String


month m startDay maxDay = show m ++ " 2023\n" ++ week ++ spaces Sunday
where
week = "Su Mo Tu We Th Fr Sa\n"

spaces currDay | startDay == currDay = days startDay 1


| otherwise = " " ++ spaces (next currDay)

days Sunday n | n > maxDay = "\n"


days _ n | n > maxDay = "\n\n"
days Saturday n = pad n ++ "\n" ++ days Sunday (succ n)
days day n = pad n ++ " " ++ days (next day) (succ n)
year = month January Thursday 31
++ month February Wednesday 28
++ month March Wednesday 31
++ month April Saturday 30
++ month May Monday 31
++ month June Thursday 30
++ month July Saturday 31
++ month August Tuesday 31
++ month September Friday 30
++ month October Sunday 31
++ month November Wednesday 30
++ month December Friday 31

main = putStr year


Output:
[?2004l
January 2023
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

February 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28

March 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

April 2023
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

May 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

June 2023
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

July 2023
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

August 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

September 2023
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

October 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

November 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

December 2023
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

[?2004h

3. HASKELL LIST GENERATION


Program:
import Data.List
main = do
let x = [1..10]
let y = [100..110]
let names = ("Afifah", "Asmi", "Reena", "Kajol")
print (x)
print (names)
print (reverse x)
print (reverse y)
print (tail x)
print (head y)
print (maximum x)
print (minimum y)
print (sum x)
print (product x)
print $ transpose ["Good", "Days"]
print $ concat ["Come", "Soon"]
print $ sort ["California", "Afifah", "Asmi", "Reena", "Kajol"]
print $ reverse ["London", "New York", "Paris", "Singapore", "Mumbai"]
print $ permutations "lion"
print $ head x
print $ tail y
print $ length x

Output:
[?2004l
[1,2,3,4,5,6,7,8,9,10]
("Afifah","Asmi","Reena","Kajol")
[10,9,8,7,6,5,4,3,2,1]
[110,109,108,107,106,105,104,103,102,101,100]
[2,3,4,5,6,7,8,9,10]
100
10
100
55
3628800
["GD","oa","oy","ds"]
"ComeSoon"
["Afifah","Asmi","California","Kajol","Reena"]
["Mumbai","Singapore","Paris","New York","London"]
["lion","ilon","oiln","ioln","olin","loin","noil","onil","oinl","niol","inol","ionl","nlio","lnio","lin
o","nilo","inlo","ilno","nloi","lnoi","loni","noli","onli","olni"]
1
[101,102,103,104,105,106,107,108,109,110]
10
[?2004h

4. HASKELL SOFTWARE FOR BANKING TRANSACTION


Program:
import Control.Concurrent.STM

type Account = TVar Integer

credit account amount = do


current <- readTVar account
writeTVar account (current + amount)

debit account amount = do


current <- readTVar account
writeTVar account (current - amount)

transfer from to amount =


atomically $ do
debit from amount
credit to amount

main = do

vaishnaviaccount <- atomically $ newTVar 2000000


shrutiaccount <- atomically $ newTVar 3000000
balance1 <- atomically $ readTVar vaishnaviaccount
balance2 <- atomically $ readTVar shrutiaccount

transfer vaishnaviaccount shrutiaccount 50000

balance3 <- atomically $ readTVar vaishnaviaccount


balance4 <- atomically $ readTVar shrutiaccount

print $ concat["Current " , "Balance ", "in " , "Vaishnavi Kale Account:" ]
print balance1
print $ concat["Current " , "Balance ", "in " , "Shruti Rai Account:" ]
print balance2
print $ concat["Current " , "Balance ", "in " , "Vaishnavi Kale Account:" ]
print balance3
print $ concat["Current " , "Balance ", "in " , "Shruti Rai Account:" ]
print balance4

Output:
"Current Balance in Vaishnavi Kale Account:"
2000000
"Current Balance in Shruti Rai Account:"
3000000
"Current Balance in Vaishnavi Kale Account:"
1950000
"Current Balance in Shruti Rai Account:"
3050000

5. ARITHMETIC OPERTIONS IN PROLOG

Addition is 56 + 65

Addition = 121
Subtraction is 2000 - 650

Subtraction = 1350
Multiplication is 70 * 45
Multiplication = 3150
Division is 250/5

Division = 50
Power is 5 ** 10

Power = 9765625
Square is 5 ** 10

Square = 9765625
Cube is 5 ** 10

Cube = 9765625
InDiv is 5 ** 10

InDiv = 9765625
IntDiv is 5 ** 10

IntDiv = 9765625
Modulus is 550 mod 10

Modulus = 0
Modulus is 55 mod 10

R = 776,
Squareroot = 27.85677655436824
R is 726, Squareroot is sqrt(R).

R = 726,
Squareroot = 26.94438717061496
R is 786, Squareroot is sqrt(R).

R = 786,
Squareroot = 28.035691537752374
R is 729, Squareroot is sqrt(R).

R = 729,
Squareroot = 27.0
R is 675, S is 896, T is max(R,S).

R = 675,
S = T, T = 896, T is max(R,S).

R = 675,
S = T, T = 896
R is 987, S is 345, T is min(R,S).

R = 987,
S = T, T = 345
19 * 2 > 78 + 20.

false
19 * 2 =:= 30 + 8.

1true
138 * 834 =\= 102 + 820

1true

6. FAMILY TREE GENERATION IN PROLOG


Program:
male(nobita).
male(doremon).
male(suneo).
male(gian).
female(shizuka).
female(doremi).
female(miichan).
female(jaiko).

parent_of(nobita,gian).
parent_of(shizuka,suneo).
parent_of(nobita,jaiko ).
parent_of(suneo, doremon).
parent_of(miichan,doremii).
parent_of(jaiko, miichan).

father_of(X,Y):- male(X),
parent_of(X,Y).

mother_of(X,Y):- female(X),
parent_of(X,Y).

grandfather_of(X,Y):- male(X),
parent_of(X,Z),
parent_of(Z,Y).

grandmother_of(X,Y):- female(X),
parent_of(X,Z),
parent_of(Z,Y).

sister_of(X,Y):-
female(X),
father_of(F, Y), father_of(F,X),X \= Y.

sister_of(X,Y):- female(X),
mother_of(M, Y), mother_of(M,X),X \= Y.

aunt_of(X,Y):- female(X),
parent_of(Z,Y), sister_of(Z,X),!.

brother_of(X,Y):-
male(X),
father_of(F, Y), father_of(F,X),X \= Y.

brother_of(X,Y):- male(X),
mother_of(M, Y), mother_of(M,X),X \= Y.

uncle_of(X,Y):-
parent_of(Z,Y), brother_of(Z,X).

Output:

mother_of(X,Y)
X = shizuka,
Y = suneo
X = miichan,
Y = doremii
X = jaiko,
Y = miichan

father_of(X,Y)
X = nobita,
Y = gian
X = nobita,
Y = jaiko
X = suneo,
Y = doremon

grandfather_of(X,Y)
X = nobita,
Y = miichan

grandmother_of(X,Y)
X = shizuka,
Y = doremon
X = jaiko,
Y = doremii

sister_of(X,Y)
X = jaiko,
Y = gian
brother_of(X,Y)
X = gian,
Y = jaiko

brother_of(X,Y)
X = gian,
Y = jaiko

uncle_of(X,Y)
false

aunt_of(X,Y)
false

7. LIST OPERATIONS IN PROLOG


Program:
list_length([],0).
list_length([_| Elements], Length) :- list_length(Elements, Length1),
Length is Length1 + 1.

list_delete(X,[X], []).
list_delete(X,[X | L1], L1).
list_delete(X,[Y | L2], [Y | L1]):- list_delete(X, L2, L1).

list_concat([], L, L).
list_concat([X1 | L1], L2,[X1 | L3]):- list_concat(L1, L2, L3).

list_rev([], []).
list_rev([Head|Tail], Reversed):- list_rev(Tail, RevTail),
list_concat(RevTail, [Head], Reversed).

join_list([], L2, L2).


join_list([X | L1], L2, [X | L3]) :-
join_list(L1, L2, L3).

insert_at_end(L, X, NewList) :-
join_list(L, [X], NewList).

dividing_list([],[],[]).
dividing_list([X],[X],[]).
dividing_list([X,Y|Tail], [X|List1],[Y|List2]) :-
dividing_list(Tail,List1,List2).

Output:
list_length([a,b,c,g,y,f],Length).
Length = 6

list_delete(b,[a,b,c,g,y,f],New_List).
New_List = [a, c, g, y, f]

list_rev([p,p,a,s,t,a,h,w],Reversed).
Reversed = [w, h, a, t, s, a, p, p]
join_list([ram,sham],[magan,jagan], Combined_List).
Combined_List = [ram, sham, magan, jagan]

insert_at_end([320,325,339],345,ElementAddedAtLast).
ElementAddedAtLast = [320, 325, 339, 345]

dividing_list([1,b,4,f,6,y,9,j,3,d,5,u],Numbers,Letters).
Letters = [b, f, y, j, d, u],
Numbers = [1, 4, 6, 9, 3, 5]

8. PROLOG USING MERGESORT


Program:
merge_sort([], []).
merge_sort([X], [X]).
merge_sort(List, Sorted) :-
List = [,|_],
divide(List, Left, Right),
merge_sort(Left, SortedLeft),
merge_sort(Right, SortedRight),
merge(SortedLeft, SortedRight, Sorted).
divide([], [], []).
divide([X], [X], []).
divide([X,Y|Rest], [X|Left], [Y|Right]) :-
divide(Rest, Left, Right).
merge([], Right, Right).
merge(Left, [], Left).
merge([X|Left], [Y|Right], [X|Merged]) :-
X =< Y,
merge(Left, [Y|Right], Merged).
merge([X|Left], [Y|Right], [Y|Merged]) :-
X > Y,
merge([X|Left], Right, Merged).

Output:
merge_sort([20, 56, 46, 10, 1 34], Sorted).
Sorted = [10, 20, 46, 56, 134]
false
merge_sort([1967, 2005,1990,2024], Sorted).
Sorted = [1967, 1990, 2005, 2024]
false

9. LIST REPRESENTATION IN PROLOG


Program:
% Start with an empty database
:- dynamic(friend/2).

% Sample facts
friend(alice, bob).
friend(bob, charlie).

% Add a new friend


add_friend(X, Y) :-
assert(friend(X, Y)).
% Remove a friend
remove_friend(X, Y) :-
retract(friend(X, Y)).

% List all friends


list_friends :-
friend(X, Y),
format('~w is friends with ~w~n', [X, Y]),
fail.

Output:
list_friends

alice is friends with bob


bob is friends with charlie
false
remove_friend(alice, charlie)

false
remove_friend(alice, bob)

1true
add_friend(ritik,rahul)

1true

10. IMPLEMENT DATABASE MANIPULATIONS IN PROLOG


Program:
- dynamic(friend/2).
% Sample facts
friend(yash, jay).
friend(janhavi, vaishnavi).
% Add a new friend at the beginning of the list
add_friend(X, Y) :-
asserta(friend(X, Y)).
% List all friends
list_friends :-
friend(X, Y),
format('~w is friends with ~w~n', [X, Y]),
fail.
list_friends.
?- add_friend(kumud, vrushali).
?- add_friend(ari, ankita).

Output:
list_friends.
ari is friends with ankita
kumud is friends with vrushali
yash is friends with jay
janhavi is friends with vaishnavi
1true

Program:
:- dynamic(friend/2).
% Sample facts
friend(yash, jay).
friend(janhavi, vaishnavi).
% Add a new friend at the beginning of the list
add_friend(X, Y) :-
assertz(friend(X, Y)).
% List all friends
list_friends :-
friend(X, Y),
format('~w is friends with ~w~n', [X, Y]),
fail.
list_friends.
?- add_friend(kumud, vrushali).
?- add_friend(ari, ankita).

Output:
list_friends.
yash is friends with jay
janhavi is friends with vaishnavi
kumud is friends with vrushali
ari is friends with ankita
1true

You might also like