Sie sind auf Seite 1von 31

Contents

Overview................................................................................................................ 2
Haskell Application.............................................................................................. 2
Prolog Application............................................................................................... 3
Family Tree....................................................................................................... 3
Screen Shots.......................................................................................................... 4
Haskell Application.............................................................................................. 4
Prolog Application............................................................................................... 9
System Limitations & Enhancement.....................................................................12
Test Plan............................................................................................................... 14
Haskell Application............................................................................................ 14
i)

Main Menu............................................................................................... 14

ii)

Metric Conversion Menu..........................................................................14

iii) Currency Conversion Menu......................................................................16


iv) Binary Conversion Menu..........................................................................17
Prolog Application............................................................................................. 18
Source Codes....................................................................................................... 22
Haskell Application............................................................................................ 22
i)

converter Function................................................................................... 22

ii)

metricCon, currencyCon and binaryCon Functions..................................23

iii) Higher Order Function.............................................................................. 27


iv) Recursion................................................................................................. 27
v)

Reverse and Concat Functions.................................................................27

Prolog Application............................................................................................. 28
i)

Facts........................................................................................................ 28

ii)

Rules........................................................................................................ 28

Conclusion............................................................................................................ 29
Reference............................................................................................................. 30

Overview
With the rise in demand for skilled computer programmers, it is essential for
computing students to be proficient in the various types programming paradigms that exist
including, functional and logical programming. Generally, the aim of this assignment is to test
students on their understanding of the two programming languages covered in the Advanced
Programming Language Concepts module syllabus, which are, Haskell and Prolog, by
developing two simple applications using Haskell for functional programming concepts and
Prolog for logical programming concepts. In the assignment question, it is specifically stated
that students must be able to implement primitive recursion and higher-order functions for the
Haskell application, with the use of menus as well as to express queries, facts and rules for the
Prolog application. The applications are discussed briefly in the section below.

Haskell Application
The application that has been selected to demonstrate the use of functional programming
concepts is a simple conversion tool where users will possess the ability to enter a number to
be converted. The modes of conversion that have been determined to be incorporated as the
functions of the application are Metric, Currency and Binary conversions.
These conversions will make up the main menu of the application. These three types of
conversions will then be followed by subsequent menus that will provide options for the users
to successfully carry out the conversion. After selecting Option 1 in the main menu, which is
metric conversion, users will then be prompted to select what type of metric measurement
they would like to use to convert a number into. There will be four measurement options to
choose from, which are, Length, Temperature, Mass and Time. Under these four
measurement types are further menus that will allow users to choose the unit of conversion for
each measurement. If users choose Option 2, which is the option for temperature, the
temperature unit conversion menu will be displayed where users can choose from
Celsius/Fahrenheit, Fahrenheit/Kelvin and Kelvin/Celsius. When users choose Option 3,
they will be prompted to enter a number to be converted and the number entered will be
converted from Kelvin to degree Celsius and vice-versa. The converted number will then be
displayed in the two units chosen.

This is just one example to demonstrate how the application will work with the incorporation
of a menu-based interface. The rest of the menu options will be discussed in the further
sections of this report. Apart from that, to ensure that the requirements of the assignment is
met, functional programming concepts, mainly, higher order functions and recursion, will be
included in the coding to develop the application.

Prolog Application
The application that has been selected to be developed for the demonstration of logical
programming is a genealogy application to capture family relations, with the help of a family
tree. Features such as facts, rules, and queries, which constitute a major part of Prolog, will be
applied in the development of the application. The family tree shows how people are related
to each other. Using logic, a series of rules based on the facts of the family tree will be created
and queries will be made to test the rules. The family tree which has been created for the
application is shown below.
Family Tree

Screen Shots
Haskell Application
A detailed explanation of how the application will work in WinHugs is provided in the section
below.

The screenshot above shows how the application works. From the user input, it can be seen
that the user has selected gram to ounce conversion.

The screen shot above shows how the application works when the user has selected their
preferred unit of conversion. They will be prompted to enter a number and the number will be
converted from gram to ounce and vice-versa before being displayed.

The screenshot above shows how the application works when the user selects option number
2 which is currency conversion. They will be prompted to select which currency they would
prefer to convert the Malaysian Ringgit into. Here, the user has selected option number 5
which is British Pound Sterling. They will be then prompted to enter the amount to be
converted. After the amount has been entered, a display message will show the conversion
from Ringgit to Pounds and vice-versa.

The screenshot above demonstrates how binary conversion works in a straight-forward


manner. Here, the user has opted to convert from binary to decimal.

For decimal to binary conversion, users are prompted to enter the binary function which is
toBin, followed with the decimal number that they would like to convert in order for the
conversion to take place. The number in binary representation will then be displayed.

Prolog Application

The screenshot above tests the father and mother queries. It displays all the possible father
child and mother child relationship that exists from the facts and rules stated.

The screenshot above tests the grandfather and grandmother queries. It displays all the
possible grandfather grandchild and grandmother grandchild relationship that exists from
the facts and rules stated.

The screenshot above tests the brother and sister queries. It displays all the possible brother
sister relationship that exists from the facts and rules stated.

10

The screenshot above tests the husband and wife queries. It displays all the possible husband
wife relationship that exists from the facts and rules stated.

The screenshot above tests the uncle and aunt queries. It displays all the possible uncle
nephew/niece and aunt nephew/niece relationship that exists from the facts and rules stated.
11

The screenshot above tests the father-in-law and mother-in-law queries. It displays all the
possible father-in-law son/daughter-in-law and mother-in-law son/daughter-in-law
relationship that exists from the facts and rules stated.

12

System Limitations & Enhancement

There will obviously be certain limitations to the application in terms of what it can
achieve and how does it perform. For the metric conversion, one of the most integral elements
is the metric measurement type. The application limits this to only four of the most common
types (length, temperature, mass and time). The conversion units for each measurement type
are also limited to a predetermined set to ensure that the application is not too big. As for the
currency conversion, one limiting factor that exists is that users may only choose to convert
Malaysian Ringgit into five other currency types and vice-versa. Another limitation that can
be seen is when users enter a wrong input. This is deemed to be very cumbersome because
each time a wrong input is entered, the application has to be restarted from the beginning.
Enhancement of the applications features and capabilities is essential so that it
conforms to users expectations. With that being said, the application may be increased in size
by adding more conversion functions as well as more measurement types, units, and
currencies as it will provide users with a wide array of conversion choices to choose from.
Occurrences of error will also be handled in a proper manner as it will help in enhancing how
the system reacts to error and what can be done to ensure ease of use.

13

Test Plan
Haskell Application
i)

Case
No.
1
2

Main Menu

Task
Select type of conversion
Enter number: 1

Enter number: 2

Enter number: 3

ii)

Case
No.
1
2
2.1
2.2
2.2.1
2.3
2.3.1

2.4
2.4.1

3
3.1
3.2

Expected Result
Display Main Menu
Display You chose option number 1
which is METRIC conversion
Display You chose option number 2
which is CURRENCY conversion
Display You chose option number 3
which is BINARY conversion

Actual Result
Result as expected
Result as expected
Result as expected
Result as expected

Metric Conversion Menu

Task
Select preferred
measurement conversion
Enter number: 1
Select preferred unit of
conversion
Enter number: 1
(Meter/Yard)
Enter number to be
converted: 21
Enter number: 2
(Kilometer/Mile)
Enter number to be
converted: 6
Enter number: 3
(Centimeter/Inch)
Enter number to be
converted: 28
Enter number: 2
Select preferred unit of
conversion
Enter number: 1
(Celsius/Fahrenheit)

Expected Result

Actual Result

Display Metric Measurement Menu

Result as expected

Display You chose option number 1


which is LENGTH measurement
Display Length Unit Menu

Result as expected

Display Enter number to be converted:

Result as expected

Display 21.0 meters is 22.96581 yards


21.0 yards is 19.2024 meters
Display Enter number to be converted:

Result as expected

Display 6.0 kilometers is 3.728226


miles
6.0 miles is 9.65604 kilometers
Display Enter number to be converted:

Result as expected

Result as expected

Result as expected

Result as expected

Display 28.0 centimeters is 11.02363


Result as expected
inches
28.0 inches is 71.12 centimeters
Display You chose option number 2
Result as expected
which is TEMPERATURE measurement
Display Temperature Unit Menu
Result as expected
Display Enter number to be converted:

Result as expected

14

3.2.1

Enter number to be
converted: 11

3.3

Enter number: 2
(Fahrenheit/Kelvin)
Enter number to be
converted: 9

3.3.1

3.4
3.4.1

4
4.1
4.2

Enter number: 3
(Kelvin/Celsius)
Enter number to be
converted: 12
Enter number: 3
Select preferred unit of
conversion
Enter number: 1
(Kilogram/Stone)

4.2.1

Enter number to be
converted: 33

4.3

Enter number: 2
(Kilogram/Pound)

4.3.1

Enter number to be
converted: 17

4.4

Enter number: 3
(Gram/Ounce)
Enter number to be
converted: 45
Enter number: 4

4.4.1
5
5.1
5.2

5.2.1

5.3

Select preferred unit of


conversion
Enter number: 1
(Second/Minute)
Enter number to be
converted: 101
Enter number: 2

Display 11.0 degrees Celsius is 51.8


degrees Fahrenheit
11.0 degrees Fahrenheit is -11.66667
degrees Celsius
Display Enter number to be converted:

Result as expected

Result as expected

Display 9.0 degrees Fahrenheit is


Result as expected
260.3722 Kelvin
9.0 Kelvin is -443.47 degrees Fahrenheit
Display Enter number to be converted: Result as expected
Display 12.0 Kelvin is -261.15 degrees
Celsius
12.0 degrees Celsius is 285.15 Kelvin
Display You chose option number 3
which is MASS measurement
Display Mass Unit Menu

Result as expected

Display Enter number to be converted:

Result as expected

Display 33.0 kilograms is 5.196609


stones
33.0 stones is 209.5596 kilograms
Display Enter number to be converted:

Result as expected

Display 17.0 kilograms is 37.47854


pounds
17.0 pounds is 7.711064 kilograms
Display Enter number to be converted:

Result as expected

Display 45.0 grams is 1.58733 ounces


45.0 ounces is 1275.728 grams
Display You chose option number 3
which is TIME measurement
Display Time Unit Menu

Result as expected

Display Enter number to be converted:

Result as expected

Display 101.0 seconds is 1.68367


minutes
101.0 minutes is 6060.0 seconds
Display Enter number to be converted:

Result as expected

Result as expected
Result as expected

Result as expected

Result as expected

Result as expected
Result as expected

Result as expected
15

5.3.1

5.4
5.4.1

iii)

Case
No.
1
2

(Minute/Hour)
Enter number to be
converted: 27
Enter number: 3
(Second/Hour)
Enter number to be
converted: 8

Display 27.0 minutes is 0.45009 hours


27.0 hours is 1620.0 minutes

Result as expected

Display Enter number to be converted:

Result as expected

Display 8.0 seconds is 0.0022224 hours


8.0 hours is 28800.0 seconds

Result as expected

Currency Conversion Menu

Task
Select Malaysian Ringgit
currency conversion
Enter number: 1

2.1

Enter amount: 57

Enter number: 2

3.1

Enter amount: 32

Enter number: 3

4,1

Enter amount: 16

Enter number: 4

5.1

Enter amount: 48

Enter number: 5

Expected Result

Actual Result

Display Currency Conversion Menu

Result as expected

Display You chose option number 1


which is US DOLLAR conversion
Enter Malaysian Ringgit to be converted
to US Dollar:
Display 57.0 Ringgit is 17.1228 US
Dollars
57.0 US Dollars is 189.696 Ringgit
Display You chose option number 2
which is EURO conversion
Enter Malaysian Ringgit to be converted
to Euro:
Display 32.0 Ringgit is 7.1776 Euros
32.0 Euros is 142.6432 Ringgit
Display You chose option number 3
which is INDIAN RUPEE conversion
Enter Malaysian Ringgit to be converted
to Indian Rupee:
Display 16.0 Ringgit is 326.88 Rupees
16.0 Rupees is 0.8 Ringgit
Display You chose option number 4
which is CHINESE YUAN conversion
Enter Malaysian Ringgit to be converted
to Chinese Yuan:
Display 48.0 Ringgit is 88.2768 Yuan
48.0 Yuan is 26.0976 Ringgit
Display You chose option number 5
which is BRITISH POUND STERLING
conversion
Enter Malaysian Ringgit to be converted
to British Pound Sterling:

Result as expected

Result as expected

Result as expected

Result as expected
Result as expected

Result as expected
Result as expected

Result as expected
Result as expected

16

6.1

iv)

Case
No.
1
2

2.1

3.1

Enter amount: 64

Display 64.0 Ringgit is 12.3648 Pounds


64.0 Pounds is 331.2128 Ringgit

Result as expected

Binary Conversion Menu

Task

Expected Result

Actual Result

Select preferred binary


conversion
Enter number: 1

Display Binary Conversion Menu

Result as expected

Display You chose option number 1


which is BINARY to DECIMAL
conversion
Enter a binary number:

Result as expected

Enter binary number:


1011001

Display "1011001" in decimal


representation is 89

Result as expected

Enter number: 2

Display You chose option number 2


which is DECIMAL to BINARY
conversion
Enter the binary function (toBin),
followed with a decimal number:

Result as expected

Enter binary function


toBin with decimal
number: 37

Display "100101"

Result as expected

17

Prolog Application
Case
No.
1

Query
?- father(X,Y).

?-mother(X,Y).

?- grandfather(X,Y).

Expected Result
X = john,
Y = lily ;
X = john,
Y = cindy ;
X = tom,
Y = kate ;
X = paul,
Y = anna ;
X = paul,
Y = lisa ;
X = john,
Y = william ;
X = tom,
Y = harry ;
X = william,
Y = mike.
X = celine,
Y = lily ;
X = celine,
Y = cindy ;
X = lily,
Y = kate ;
X = kate,
Y = anna ;
X = kate,
Y = lisa ;
X = celine,
Y = william ;
X = lily,
Y = harry ;
X = mary,
Y = mike.
X = john,

Actual result
Result as expected

Result as expected

Result as expected
18

Y = kate ;
X = john,
Y = harry ;
X = tom,
Y = anna ;
X = tom,
Y = lisa ;
X = john,
Y = mike ;

Case
No.
4

Query
?- grandmother(X,Y)

?- brother(X,Y).

?- sister(X,Y).

Expected Result
X = celine,
Y = kate ;
X = celine,
Y = harry ;
X = lily,
Y = anna ;
X = lily,
Y = lisa ;
X = celine,
Y = mike ;
X = harry,
Y = kate ;
X = william,
Y = lily ;
X = william,
Y = cindy ;
X = lily,
Y = cindy ;
X = lily,
Y = william ;
X = anna,
Y = lisa ;
X = kate,
Y = harry ;
X = lisa,
Y = anna ;
X = cindy,
Y = lily ;
X = cindy,
Y = william ;

Actual result
Result as expected

Result as expected

Result as expected

19

?-husband(X,Y).

Query

X = john,
Y = celine ;
X = paul,
Y = kate ;
X = tom,
Y = lily ;
X = william,
Y = mary ;

Case
No.
8

Expected Result

?-wife(X,Y).

X = celine,
Y = john ;
X = lily,
Y = tom ;
X = mary,
Y = william ;
X = kate,
Y = paul ;

?-uncle(X,Y).

10

?-aunt(X,Y).

X = william,
Y = kate ;
X = harry,
Y = anna ;
X = harry,
Y = lisa ;
X = william,
Y = harry ;
X = lily,

X = john,
Y = celine ;
X = john,
Y = celine ;
X = john,
Y = celine ;
X = paul,
Y = kate ;
X = paul,
Y = kate ;
X = tom,
Y = lily ;
X = tom,
Y = lily ;
X = william,
Y = mary ;
Actual result
X = celine,
Y = john ;
X = celine,
Y = john ;
X = celine,
Y = john ;
X = lily,
Y = tom ;
X = lily,
Y = tom ;
X = mary,
Y = william ;
X = kate,
Y = paul ;
X = kate,
Y = paul ;
Result as expected

Result as expected
20

11

Case
No.
12

?-fatherinlaw(X,Y).

Query
?-motherinlaw(X,Y).

Y = mike ;
X = cindy,
Y = mike ;
X = cindy,
Y = kate ;
X = cindy,
Y = harry ;
X = tom,
Y = paul ;
X = john,
Y = tom ;
X = john,
Y = mary ;

Expected Result
X = lily,
Y = paul ;
X = celine,
Y = tom ;
X = celine,
Y = mary ;

X = tom,
Y = paul ;
X = tom,
Y = paul ;
X = john,
Y = tom ;
X = john,
Y = tom ;
X = john,
Y = mary ;

Actual result
X = lily,
Y = paul ;
X = lily,
Y = paul ;
X = celine,
Y = tom ;
X = celine,
Y = tom ;
X = celine,
Y = mary ;

21

Source Codes
Haskell Application
i)

converter Function

The converter function is created to hold the options for users to select which type of
conversion that they would like the application to perform. This function represents the Main
Menu of the application. To run the application, this function must be called.

22

ii)

metricCon, currencyCon and binaryCon Functions

As the names suggest, these functions represent the metric, currency and binary operations
respectively. We can also see from the coding below how menus and sub-menus are used as a
structure for the application. Switch case statements and if..else statements are applied for
selection control.

23

24

25

26

iii)

Higher Order Function

foldl is applied in the toDec function as can be seen in the code snippet below to reduce the
binary list from left to right.
iv)

Recursion

The toBin function is a prime example of a recursive function which calls itself.
v)

Reverse and Concat Functions

The reverse function is used to reverse the order of the binary number once it has been
converted so that it appears in the correct order while the concat function joins the list of ones
and zeros into a string.
27

Prolog Application
i)

Facts

28

ii)

Rules

Conclusion
With the completion of this assignment, I can conclude that it has been very beneficial
to me as being a final-year student as I have gained a lot of insights on how to apply
29

functional and logical programming concepts to produce a satisfactory application that meets
the requirements.I am now able to better comprehend the ins and outs of how functional as
well as logical programming work and that has definitely hone my programming skills. The
knowledge that I have gained is immense and will surely be advantageous for me in the future
because this project that I have worked on intently provides some insights of what should be
expected when I make the transition into the working world. I am now aware of as to what
Haskell and Prolog programming criteria would shape a good program and how do I go about
to achieve those criteria.
To accomplish the requirements of this assignment, I did plenty of research and
planning on how to develop two types of applications which are different to one another in
terms of the programming paradigm and how they are applied in the real world. By doing so,
my research and programming skills has also been refined. The development of the two
applications has also resulted in a better understanding of the processes involved in designing
and analysing proper functional and logical aspects of the programming languages as well as
practicing good coding styles. With the knowledge that I have gained, I will be better
equipped for the future.

Reference
Learnprolognow.org. 2006. Learn Prolog Now!. [online] Available at:
http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse49
[Accessed: 1 Sep 2013].
Learnyouahaskell.com. 2000. Recursion - Learn You a Haskell for Great Good!. [online]
Available at: http://learnyouahaskell.com/recursion#hello-recursion [Accessed: 29 Aug
2013].
30

Thompson, S. 1999. Haskell. Harlow [u.a.]: Addison-Wesley.

31

Das könnte Ihnen auch gefallen