You are on page 1of 29

INDIVIDUAL ASSIGNMENT

BY
PRIMAL PRASANGA WADANAMBY (CB003492)
MODULE TITLE
ADVANCED PROGRAMMING LANGUAGE CONCEPTS
MODULE CODE
CE00331-3
INTAKE CODE
GF1171COM
LECTURER
MR. GAMINDU HEMACHANDRA
DATE ASSIGNED
13TH DECEMBER 2011
DATE DUE
8TH FEBRUARY 2012

APLC CE00331-3

Individual Assignment

ABSTRACT
Advanced programming language concepts are mainly based on paradigms which can be
use to solve a problem programmatically. It differ the style, concept and events which
facilitate the programmer to solve a programming problem. These facilities are highly
useful to programmers that allow them to find suitable approach to the address the
problem and to ease their work.
Function programming is an approach which is mentioned along with this module as well
as its assignment. Haskell as the programming language of elaboration of functional
programming concepts, a program which calculates the primary calculations of an IP
address calculator is implemented in first chapter.
Second chapter represents the relationships between owners of vehicles and their
past/current ownerships. It is programmed with using prolog programming language
which is similarly a functional programming language.

APLC CE00331-3

Individual Assignment

ACKNOWLEDGEMENT
I would like to extend my sincere gratitude for our lecturer, Mr. Gamindu Hemachandra,
for his continuous advice and guidance given to me throughout this module. Without his
immense guidance and supervision, it would be hard for me to continue studies properly.
My heartfelt gratitude also goes to my parents for their continuous support guiding me in
all aspects throughout my carrier.
My thanks also go to the administration of APIIT for laboratory and library facilities
provided to me whenever I needed.
Finally, the support given by my classmates is also highly appreciated.

APLC CE00331-3

Individual Assignment

Table of Contents

ABSTRACT.........................................................................................................................ii
ACKNOWLEDGEMENT..................................................................................................iii
Table of Contents................................................................................................................iv
List of Figures......................................................................................................................v
List of Abbreviations............................................................................................................v
1.0

Haskell.......................................................................................................................1

1.1 System Overview.......................................................................................................1


1.2 Screenshots.................................................................................................................2
1.3 Limitations and Enhancements..................................................................................7
1.4 Test Plan.....................................................................................................................8
1.6 Code.........................................................................................................................12
2.0

Prolog......................................................................................................................20

2.1 System Overview.....................................................................................................20


2.2 Facts.........................................................................................................................20
2.3 Rules.........................................................................................................................22
3.0

Conclusion...............................................................................................................23

4.0 References and Bibliography.......................................................................................24

APLC CE00331-3

Individual Assignment

List of Figures
Figure 1 Main menu..........................................................................................................2
Figure 2 Data conversion Menu (Binary/Octal/Hexadecimal to Decimal).......................3
Figure 3 Generate Default Subnet Mask Screen...............................................................4
Figure 4 Network/Host addresses Generate Menu............................................................5
Figure 5 - Generate Custom Subnet Mask screen................................................................6

List of Abbreviations

IP Internet Protocol

APLC CE00331-3

Individual Assignment

1.0 Haskell
1.1 System Overview
IP Address Calculator is a program which is developed to do basic functions of
calculations that are required to assigning IP addresses to a network. Mainly the system
based on the process of generating a custom subnet mask for a given IP address followed
by other general functions such as,

Data conversion,
o Binary to decimal conversion
o Octal to decimal conversion
o Hexadecimal to decimal conversion
Class of an IP address
Default subnet mask of an IP address
Network and Host IP of an IP address

Function overview:
1) Binary to decimal conversion
10001101,00111001,00010111,00100011 -> 141, 57, 23, 35
2) Octal to decimal conversion
234, 141, 171, 57 -> 156, 97, 121, 47
3) Hexadecimal to decimal conversion
67, 55, 37, 40 -> 103, 85, 55, 64
4) Class of an IP address
141, 57, 23, 35 Class B
5) Default subnet mask of an IP address
141, 57, 23, 35 Class B -- 255, 255, 0, 0
6) Network and Host IP of an IP address
141, 57, 23, 35 -> 141, 57, 0, 0 Network IP
-> 0, 0, 23, 35 Host IP
7) Custom Subnet Mask
Inputs:Needed usable subnets 1000
Network address 165, 100, 0, 0
Result:Custom subnet mask 255, 255, 255, 192

APLC CE00331-3

Individual Assignment

1.2 Screenshots
Screenshot Main Menu

Figure 1 Main menu

Main menu screen is mainly functional to navigate the user with preferred
functions/requirements. This menu will appear after every major function along with the
result of previous operation and enables to select another function or to exit from the
program.

APLC CE00331-3

Individual Assignment

Screenshot Data Conversion Menu

Figure 2 Data conversion Menu (Binary/Octal/Hexadecimal to Decimal)

IP addresses can be represented in many data types. Some may be in binary format and
some may in decimal or octal or hexadecimal formats. If the IP address requires any
calculation, it should be in decimal form to make the calculations easier. In this prompt,
users are welcomed to select any type of conversions of IP addresses.
The function of conversion takes each set of binary/octal/hexadecimal values (Figure 2)
into a tuple of four elements and converts them into decimal form. The final output can be
shown as IP address in decimal form.

APLC CE00331-3

Individual Assignment

Screenshot Generate Default Subnet Mask screen

Figure 3 Generate Default Subnet Mask Screen

This will facilitates the user to generate default subnet mask of a given IP address. The
function (ipCassDec) takes set of decimal values (Figure 3) into a tuple of four elements
of type integer and checks for the class of the IP address by comparing first set of decimal
values. And then the class selects the default subnet mask at the 2 nd function (difSubnet)
and returns the default subnet mask as above.

APLC CE00331-3

Individual Assignment

Screenshot Network/Host addresses Generate Menu

Figure 4 Network/Host addresses Generate Menu

This will facilitates the user to generate network or host IP address of a given IP address.
The function (ipCassDec) takes set of decimal values (Figure 4) into a tuple of four
elements of type integer and checks for the class of the IP address by comparing first set
of decimal values. And then the class will decide the brake point of network set and host
set at the 2nd function (netWorkIp/hostIp) and returns the network/host address as above.

APLC CE00331-3

Individual Assignment

Screenshot Generate Custom Subnet Mask screen

Figure 5 - Generate Custom Subnet Mask screen

This will facilitates the user to generate the custom subnet mask of a given IP address.
The function (cusSubnet) takes a count of require subnets and set of decimal values
(Figure 5) into a tuple of four elements of type integer and checks for the class of the IP
address along with its range of custom subnet range by comparing the number of required
subnets. And then the function will select the range of the custom subnet and returns it as
above.

APLC CE00331-3

Individual Assignment

1.3 Limitations and Enhancements


Limitations

User cannot enter an IP address as a single set of value.


Ex: - 127.104.40.21 cannot be used

Any number of digits can be added as the input of data form conversion.
(Validations are not included).
Ex: - 1000111001110 can be inserted

IP addresses should be in decimal format in order to use major functions other


than conversions.

Enhancements

Functions can be added with many operations.


IP address can be taken into a one list which eases to use in every function.
Sub-netting operations can be added.

APLC CE00331-3

Individual Assignment

1.4 Test Plan


Functionality 1: IP Address Conversion (Binary/Octal/Hexadecimal to Decimal)
Test Inputs: Binary to decimal conversion 10001101,00111001,00010111,00100011
Octal to decimal conversion
234, 141, 171, 57
Hexadecimal to decimal conversion
67, 55, 37, 40

Functionality Description: Binary/Octal/Hexadecimal to decimal conversion


The functions, ipConvertBin, ipConvertOct and ipConvertHex of conversion
should take each set of binary, octal and hexadecimal values the type of string into a tuple
of four elements. Then the each element of the tuple should convert into decimal form
returns as a tuple with elements in integer data type.
Conversion needs to be taken place at the functions called binToDec,
octToDec and hexToDec which takes each element of the tuple as strings and read
and calculate digit by digit. At the end it should return the converted values as a tuple
back to the functions ipConvertBin, ipConvertOct and ipConvertHex.

Result: Converted successfully


Binary to decimal conversion 10001101,00111001,00010111,00100011 -> 141, 57, 23, 35
Octal to decimal conversion
234, 141, 171, 57 -> 156, 97, 121, 47
Hexadecimal to decimal conversion

APLC CE00331-3

Individual Assignment

67, 55, 37, 40 -> 103, 85, 55, 64


Functionality 2: IP Class Check
Test Inputs: Class of an IP address 141, 57, 23, 35

Functionality Description: The function ipClassDec should take each set of decimal values the type of
integer into a tuple of four elements. Then the first element of the tuple should check with
the default values assigned for each class and should return the class name which is in
data type Char.
Result:Checked successfully
Class of an IP address 141, 57, 23, 35 Class B

Functionality 3: Generate Default subnet mask


Test Inputs: Default subnet mask of an IP address
141, 57, 23, 35

Functionality Description: The function ipClassDec should take each set of decimal values the type of
integer into a tuple of four elements and returns the IP class to the difSubnet function.
Then the difSubnet function should select and return the default subnet mask according to
the class input of the ipClassDec function.

APLC CE00331-3

Individual Assignment

Result:Successfully Generated
Default subnet mask of an IP address
141, 57, 23, 35 --- 255, 255, 0, 0

Functionality 4: Generate Network and Host addresses


Test Inputs:Network and Host addresses of an IP address
141, 57, 23, 35 IP Address
Functionality Description: The function (ipCassDec) takes set of decimal values into a tuple of four elements
of type integer and checks for the class of the IP address by comparing first set of decimal
values. And then the class will decide the brake point of network set and host set at the
netWorkIp or hostIp functions and returns the network/host address.

Result:Successfully Generated
Network and Host addresses of an IP address
141, 57, 23, 35 IP Address
141, 57, 0, 0 Network Address
0, 0, 23, 35 Host Address

Functionality 4: Generate Custom Subnet Mask


Test Input:Custom subnet mask of an IP address
Needed usable subnets 1000
Network address 165, 100, 0, 0

10

APLC CE00331-3

Individual Assignment

Functionality Description:The cusSubnet function should take a value of required subnets and set of decimal
values into a tuple of four elements of type integer as IP address and checks for the class
of the IP address along with its range of custom subnet range by comparing the number of
required subnets. And then the function should select the range of the custom subnet and
return it.
Result:Successfully Generated
Custom subnet mask of an IP address
Needed usable subnets 1000
Network address 165, 100, 0, 0
Custom subnet mask 255, 255, 255, 192

11

APLC CE00331-3

Individual Assignment

1.6 Code
module IPCalculator where
import Data.Char
import System.Exit

---------------------------Main Menu-----------------------menuCalculator :: IO()


menuCalculator =
do
putStrLn "\n\n"
putStrLn "\n|-----------------------------------------------|"
putStrLn "\n
IP Address Calculator"
putStrLn "\n|-----------------------------------------------|"
putStrLn "\n\n Select 1: Conversion to Decimal"
putStrLn "\n Select 2: IP Class Check "
putStrLn "\n Select 3: Generate Default Subnet Mask "
putStrLn "\n Select 4: Generate Network IP / Host IP "
putStrLn "\n Select 5: Calculate Custom Subnet Mask"
putStrLn "\n Select 6: Calculate Host IP Block"
putStrLn "\n Select 0: Exit "
putStrLn "\n Option: "
input <- getLine
let choice = (read input)::Int
optionCod choice

----------------------------------------------------------------optionCod :: Int -> IO()


optionCod choice
|(choice == 1)= convertChoice
|(choice == 2)= checkIpClass
|(choice == 3)= generateDSubnet
|(choice == 4)= generateIpChoice
|(choice == 5)= generateCusSubnet
|(choice == 6)= generateSubnetBlock
|(choice == 0)= exitWith ExitSuccess
----------------------------------------------------------------convertChoice :: IO()
convertChoice =
do
putStrLn "\n\n
Conversion Types\n"
putStrLn "|-----------------------------------------------|\n\n"
putStrLn "Select 1: Binary to Decimal\n"
putStrLn "Select 2: Octal to Decimal\n"
putStrLn "Select 3: Hexadecimal to Decimal\n"
putStrLn "Select 0: Go back to Main Menu\n"
putStrLn "|-----------------------------------------------|\n"

12

APLC CE00331-3

Individual Assignment
putStrLn "Option: "
input1 <- getLine
let choice1 = (read input1)::Int
convertSelect choice1

----------------------------------------------------------------convertSelect :: Int -> IO()


convertSelect choice1
|(choice1 == 1)= binaryDecimalCon
|(choice1 == 2)= octalDecimalCon
|(choice1 == 3)= hexDecimalCon
|(choice1 == 0)= menuCalculator
----------------------------------------------------------------binaryDecimalCon :: IO()
binaryDecimalCon =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =ipConvertBin (a, b, c, d)
putStrLn("\n")
putStrLn (show result)
convertChoice
----------------------------------------------------------------octalDecimalCon :: IO()
octalDecimalCon =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result = ipConvertOct (a, b, c, d)
putStrLn("\n")
putStrLn (show result)
convertChoice
----------------------------------------------------------------hexDecimalCon :: IO()
hexDecimalCon =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine

13

APLC CE00331-3

Individual Assignment
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =ipConvertHex (a, b, c, d)
putStrLn("\n")
putStrLn (show result)
convertChoice

----------------------------------------------------------------checkIpClass :: IO()
checkIpClass =
do
putStrLn "\n\nEnter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =ipClassDec (read a,read b,read c,read d)
putStrLn("\n")
putStrLn (show result)
menuCalculator
----------------------------------------------------------------generateIpChoice :: IO()
generateIpChoice =
do
putStrLn "\n\n
IP Types\n"
putStrLn "|-----------------------------------------------|\n\n"
putStrLn "Select 1: Network IP\n"
putStrLn "Select 2: Host IP\n"
putStrLn "Select 0: Go back to Main Menu\n"
putStrLn "|-----------------------------------------------|\n"
putStrLn "Option: "
input2 <- getLine
let choice2 = (read input2)::Int
generateIpSelect choice2
----------------------------------------------------------------generateIpSelect :: Int -> IO()
generateIpSelect choice2
|(choice2 == 1)= netIpSelect
|(choice2 == 2)= hostIpSelect
|(choice2 == 0)= menuCalculator
----------------------------------------------------------------netIpSelect :: IO()
netIpSelect =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"

14

APLC CE00331-3

Individual Assignment

b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =netWorkIp ipClassDec (read a,read b,read c,read d)
putStrLn("\n")
putStrLn (show result)
generateIpChoice
----------------------------------------------------------------hostIpSelect :: IO()
hostIpSelect =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =hostIp ipClassDec (read a,read b,read c,read d)
putStrLn("\n")
putStrLn (show result)
generateIpChoice
----------------------------------------------------------------generateDSubnet::IO()
generateDSubnet =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =difSubnet ipClassDec (read a,read b,read c,read d)
putStrLn("\n")
putStrLn (show result)
menuCalculator
----------------------------------------------------------------generateCusSubnet :: IO()
generateCusSubnet =
do
putStrLn "Required Subnets :"
digit <- getLine
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"

15

APLC CE00331-3

Individual Assignment
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
let result =cusSubnet (read digit) (read a,read b,read c,read d)
putStrLn("\n")
putStrLn (show result)
menuCalculator

----------------------------------------------------------------generateSubnetBlock :: IO()
generateSubnetBlock =
do
putStrLn "Enter IP Address (x,_,_,_):"
a <- getLine
putStrLn "Enter IP Address (_,x,_,_):"
b <- getLine
putStrLn "Enter IP Address (_,_,x,_):"
c <- getLine
putStrLn "Enter IP Address (_,_,_,x):"
d <- getLine
putStrLn "Required Hosts :"
digit <- getLine
let result =ipBlock (read a,read b,read c,read d) (read digit)
putStrLn("\n")
putStrLn (show result)
menuCalculator
--------------------------FUNCTIONS---------------------------------------------FUNCTIONS - Binary to Decimal---------------binToDec :: String -> Int
binToDec = foldr (\a s -> s * 2 + a) 0 . map uc
where uc a = if a == '0' then 0 else 1

-------------------FUNCTIONS - Octal to Decimal-----------------octToDec :: String -> Int


octToDec l = sum(zipWith (*)
(map (8^)[2,1,0])
[digitToInt i|i <- l])
------------------FUNCTIONS - Headecimal to Decimal-------------hexToDec :: String -> Int
hexToDec l = sum(zipWith (*)
(map (16^)[2,1,0])
[digitToInt i|i <- l])
-------------------FUNCTIONS - Binary to Decimal-----------------

16

APLC CE00331-3

Individual Assignment

ipConvertBin::(String,String,String,String)->(Int,Int,Int,Int)
ipConvertBin(a,b,c,d)=(binToDec a,binToDec b,binToDec c,binToDec d)
ipConvertOct::(String,String,String,String)->(Int,Int,Int,Int)
ipConvertOct(a,b,c,d)=(octToDec a,octToDec b,octToDec c,octToDec d)
ipConvertHex::(String,String,String,String)->(Int,Int,Int,Int)
ipConvertHex(a,b,c,d)=(hexToDec a,hexToDec b,hexToDec c,hexToDec d)

type Ip=(Int,Int,Int,Int)
--------------------FUNCTIONS - Binary to Decimal---------------cusSubnet::Int->Ip->Ip
cusSubnet x a | (ipClassDec a)=='A' && x>0 && x<=2 =(255,128,0,0)
| (ipClassDec a)=='A' && x>=3 && x<=4 =(255,192,0,0)
| (ipClassDec a)=='A' && x>=5 && x<=8 =(255,224,0,0)
| (ipClassDec a)=='A' && x>=8 && x<=16 =(255,240,0,0)
| (ipClassDec a)=='A' && x>=17 && x<=32 =(255,248,0,0)
| (ipClassDec a)=='A' && x>=33 && x<=64 =(255,252,0,0)
| (ipClassDec a)=='A' && x>=65 && x<=128 =(255,254,0,0)
| (ipClassDec a)=='A' && x>=129 && x<=256 =(255,255,0,0)
| (ipClassDec a)=='A' && x>257 && x<=512 =(255,255,128,0)
| (ipClassDec a)=='A' && x>=513 && x<=1024
=(255,255,192,0)
| (ipClassDec a)=='A' && x>=1025 && x<=2048
=(255,255,224,0)
| (ipClassDec a)=='A' && x>=2049 && x<=4096
=(255,255,240,0)
| (ipClassDec a)=='A' && x>=4097 && x<=8192
=(255,225,248,0)
| (ipClassDec a)=='A' && x>=8193 && x<=16364
=(255,255,252,0)
| (ipClassDec a)=='A' && x>=16365 && x<=32768
=(255,255,254,0)
| (ipClassDec a)=='A' && x>=32789 && x<=65536
=(255,255,255,0)
| (ipClassDec a)=='A' && x>=65537 && x<=131072
=(255,225,255,128)
| (ipClassDec a)=='A' && x>=131072 && x<=262144
=(255,225,255,192)
| (ipClassDec a)=='A' && x>=262145 && x<=524288
=(255,225,255,224)
| (ipClassDec a)=='A' && x>=524289 && x<=1048576
=(255,225,255,240)
| (ipClassDec a)=='A' && x>=1048577 && x<=2097152
=(255,225,255,248)
| (ipClassDec a)=='A' && x>=2097153 &&
x<4194304=(255,225,255,252)
| (ipClassDec a)=='A' && x>=4194305 &&
x<8388608=(255,225,255,254)
| (ipClassDec a)=='B' && x>0 && x<=2 =(255,255,128,0)

17

APLC CE00331-3

Individual Assignment
| (ipClassDec a)=='B' && x>=3 && x<=4 =(255,255,192,0)
| (ipClassDec a)=='B' && x>=5 && x<=8 =(255,255,224,0)
| (ipClassDec a)=='B' && x>=8 && x<=16 =(255,240,240,0)
| (ipClassDec a)=='B' && x>=17 && x<=32 =(255,248,248,0)
| (ipClassDec a)=='B' && x>=33 && x<=64 =(255,252,252,0)
| (ipClassDec a)=='B' && x>=65 && x<=128 =(255,254,254,0)
| (ipClassDec a)=='B' && x>=129 && x<=256 =(255,255,255,0)
| (ipClassDec a)=='B' && x>257 && x<=512

=(255,255,255,128)
| (ipClassDec a)=='B' && x>=513 && x<=1024
=(255,255,255,192)
| (ipClassDec a)=='B' && x>=1025 && x<=2048
=(255,255,255,224)
| (ipClassDec a)=='B' && x>=2049 && x<=4096
=(255,255,255,240)
| (ipClassDec a)=='B' && x>=4097 && x<=8192
=(255,225,255,248)
| (ipClassDec a)=='B' && x>=8193 && x<=16364
=(255,255,255,252)
| (ipClassDec a)=='B' && x>=16365 && x<=32768
=(255,255,255,254)
| (ipClassDec a)=='C' && x>0 && x<=2 =(255,255,255,128)
| (ipClassDec a)=='C' && x>=3 && x<=4 =(255,255,255,192)
| (ipClassDec a)=='C' && x>=5 && x<=8 =(255,255,255,224)
| (ipClassDec a)=='C' && x>=8 && x<=16 =(255,255,255,240)
| (ipClassDec a)=='C' && x>=17 && x<=32 =(255,255,255,248)
| (ipClassDec a)=='C' && x>=33 && x<=64 =(255,255,255,252)
| (ipClassDec a)=='C' && x>=65 && x<=128
=(255,255,255,254)
--------------------FUNCTIONS - Host IP Block-------------------ipBlock::Ip->Int->[Ip]
ipBlock (a,b,c,d) y |(ipClassDec (a,b,c,d))=='A' && y>0 && y<=255 =[(a,k,0,0)|k<-[y,y+y..255]]
|(ipClassDec (a,b,c,d))=='A' && y>255 &&
y<=65536=[(a,k,r,0)|k<-[y,y+y..255],r<-[0,y..255]]
|(ipClassDec (a,b,c,d))=='A' && y>65536 &&
y<=1677216=[(a,k,r,0)|k<-[y,y+y..255],r<-[0,y..255],e<-[0,y..255]]
|(ipClassDec (a,b,c,d))=='B' && y>0 && y<=255
=[(a,b,k,0)|k<-[y,y+y..255]]
|(ipClassDec (a,b,c,d))=='B' && y>255 &&
y<=65536=[(a,b,k,r)|k<-[y,y+y..255],r<-[0,y..255]]
|(ipClassDec (a,b,c,d))=='C' && y>0 && y<=255
=[(a,b,c,k)|k<-[y,y+y..255]]
|otherwise =[(0,0,0,0)]

-------------------FUNCTIONS - Binary to Decimal----------------ipClassDec::Ip->Char


ipClassDec (a,b,c,d)
| a>0 && a<=127='A'

18

APLC CE00331-3

Individual Assignment

| a>127 && a<=191='B'


| a>191 && a<=223='C'
| a>223 && a<=239='D'
| a>240 && a<=255='E'
|otherwise =' '
---------------------FUNCTIONS - Binary to Decimal--------------difSubnet::(Ip->Char)->Ip->Ip
difSubnet f x
|(f x)=='A'=(255,0,0,0)
|(f x)=='B'=(255,255,0,0)
|(f x)=='C'=(255,255,255,0)
|otherwise =(0,0,0,0)
-------------------FUNCTIONS - Binary to Decimal----------------netWorkIp::(Ip->Char)->Ip->Ip
netWorkIp f (a,b,c,d)
|(f (a,b,c,d))=='A'=(a,0,0,0)
|(f (a,b,c,d))=='B'=(a,b,0,0)
|(f (a,b,c,d))=='C'=(a,b,c,0)
|otherwise =(0,0,0,0)
--------------------FUNCTIONS - Binary to Decimal---------------hostIp::(Ip->Char)->Ip->Ip
hostIp f (a,b,c,d)
|(f (a,b,c,d))=='A'=(0,b,c,d)
|(f (a,b,c,d))=='B'=(0,0,c,d)
|(f (a,b,c,d))=='C'=(0,0,0,d)
|otherwise =(0,0,0,0)

19

APLC CE00331-3

Individual Assignment

2.0 Prolog
2.1 System Overview
Vehicle registration is a process that combines information of both vehicle and its owner.
All these information as facts are taken into this system in order to generate information
more usefully.
Facts of a vehicle such as,

Registration number, Brand, Type, Engine Capacity, Imported Country, etc.

And facts of the owner such as,

Identity number, Name, Address, etc.

are used in the program along with rules to create more useful combinations of facts and
rules.

2.2 Facts
/* Vehicle Types */
vehicletype(car, allion).
vehicletype(car, camry).
vehicletype(car, cayenne).
vehicletype(van, caravan).
vehicletype(van, vanette).
vehicletype(lorry, elf).
/* Vehicle Brands */
brandinc(toyota, allion).
brandinc(toyota, camry).
brandinc(nissan, caravan).
brandinc(nissan, vanette).
brandinc(porche, cayenne).
brandinc(isusu, elf).
/* Imported Country */
importedfrom(japan, toyota).
importedfrom(japan, nissan).
importedfrom(usa, porche).
importedfrom(thaiwan, isusu).
/* Engine capacity */
cylindercap(1300, allion).
cylindercap(2490, camry).
cylindercap(2000, caravan).
cylindercap(3000, cayenne).
cylindercap(1300, vanette).

20

APLC CE00331-3

Individual Assignment

cylindercap(2000, elf).
/* Status when imported */
impoqual(brandnew, ha7765).
impoqual(recondition, hb7766).
impoqual(brandnew, hc7767).
impoqual(recondition, hd7768).
impoqual(brandnew, he7769).
impoqual(brandnew, hf7770).
/* Registration Number */
noplate(ha7765, allion).
noplate(hb7766, camry).
noplate(hc7767, caravan).
noplate(hd7768, vanette).
noplate(he7769, cayenne).
noplate(hf7770, elf).
/* Owners with Vehicle numbers */
regperson(sam, ha7765).
regperson(rogger, hb7766).
regperson(denis, hc7767).
regperson(albert, he7769).
regperson(gibbs, hd7768).
regperson(nathan, hf7770).
regperson(gibbs, hb7766).
regperson(sam, hd7768).
regperson(nathan, hd7768).
/* Registration dates */
regdate(2008, ha7765).
regdate(2007, hb7766).
regdate(2005, hc7767).
regdate(2012, he7769).
regdate(2004, hd7768).
regdate(2009, hf7770).
regdate(2010, hb7766).
regdate(2006, hd7768).
regdate(2010, hd7768).
personloc(malabe, sam).
personloc(hiniduma, rogger).
personloc(wadduwa, denis).
personloc(malabe, albert).
personloc(kaduwela, gibbs).
personloc(veyangoda, nathan).

21

APLC CE00331-3

Individual Assignment

2.3 Rules
/* which car which imported state*/
importedstate(X,Y):-impoqual(X,W), noplate(W,Y).
/* from which country*/
importedcountry(X,Y):-importedfrom(X,W),brandinc(W,Y).
/*which car from which country */
whichcountry(X, Y):-(noplate(X,W), brandinc(Z,W)), importedfrom(Y,Z).
/* which vehicle which capacity */
vehicapacity(X, Y):-noplate(X, W), cylindercap(Y, W).
/* which vehicle which type*/
vehitype(X, Y):-noplate(X, W), vehicletype(Y, W).
/* which vehicle which class */
vehiclass(X, Y):-noplate(X, Z), vehicleclass(Y, Z).
/* which vehicle which owner - present */
ownervehi(X, Y):- newest(X), regperson(Y, X).
newest(X):-regdate(_, X), not(previouse(X)).
previouse(X):-regdate(A, X), regdate(B, X), A>B.
/* Where the vehicle */
vehicloc(X,Y):- personloc(X,W), regperson(W,Y).

22

APLC CE00331-3

Individual Assignment

3.0 Conclusion
Functional programming paradigm is a different dimension of programming which is
more productive than the imperative programming. In most cases, programmers are
assigned in programming operations which required the extreme programming skills as
well as mathematical skills. Therefore the development requires 100% accuracy on behalf
of programmers prospective. Unlike imperative programming, functional programming
paradigm is consists with highly accurate mathematical functions which encourage the
programmers to enhance their accuracy. Eventually, the paradigm describes what the
programmer wants in simpler manner than the other programming paradigms.
Mathematical capabilities with logical programming concepts became an optimal solution
for the development of a system such as IP address calculator which consists with variety
of mathematical operations with higher accuracy.
Limitations of side effects, garbage collection, tailcall optimizations which replace the
recursion with iteration, etc. provides way beyond facilities to the system as well as to the
programmer to keep the systems efficiency continuously.

23

APLC CE00331-3

Individual Assignment

4.0 References and Bibliography


CISCO, (2005). IP Addressing and Subnetting for new users [Online] Available from:
http://www.cisco.com/en/US/tech/tk365/technologies_tech_note09186a00800a67f5.shtml
[Accessed: 3rd January 2012]
Dean, T. (2008), Network+ guide to networks, 4th Edition, Course technology, USA.
Thompson, S.(2011). Haskell the craft of functional programming, 3rd Edition, Pearson
Education Limited, England.

24

You might also like