APLCassigmtdoc

34
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 1

description

apl

Transcript of APLCassigmtdoc

Page 1: APLCassigmtdoc

ContentsOverview................................................................................................................................................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

1

Page 2: APLCassigmtdoc

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.

2

Page 3: APLCassigmtdoc

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

3

Page 4: APLCassigmtdoc

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.

4

Page 5: APLCassigmtdoc

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.

5

Page 6: APLCassigmtdoc

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.

6

Page 7: APLCassigmtdoc

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

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

7

Page 8: APLCassigmtdoc

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.

8

Page 9: APLCassigmtdoc

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.

9

Page 10: APLCassigmtdoc

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

Page 11: APLCassigmtdoc

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

Page 12: APLCassigmtdoc

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

Page 13: APLCassigmtdoc

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 application’s 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

Page 14: APLCassigmtdoc

Test Plan

Haskell Application

i) Main Menu

Case No.

Task Expected Result Actual Result

1 Select type of conversion Display Main Menu Result as expected2 Enter number: 1 Display “You chose option number 1

which is METRIC conversion”Result as expected

3 Enter number: 2 Display “You chose option number 2 which is CURRENCY conversion”

Result as expected

4 Enter number: 3 Display “You chose option number 3 which is BINARY conversion”

Result as expected

ii) Metric Conversion Menu

Case No.

Task Expected Result Actual Result

1 Select preferred measurement conversion

Display Metric Measurement Menu Result as expected

2 Enter number: 1 Display “You chose option number 1 which is LENGTH measurement”

Result as expected

2.1 Select preferred unit of conversion

Display Length Unit Menu Result as expected

2.2 Enter number: 1(Meter/Yard)

Display “Enter number to be converted: ” Result as expected

2.2.1 Enter number to be converted: 21

Display “21.0 meters is 22.96581 yards21.0 yards is 19.2024 meters”

Result as expected

2.3 Enter number: 2(Kilometer/Mile)

Display “Enter number to be converted: ” Result as expected

2.3.1 Enter number to be converted: 6

Display “6.0 kilometers is 3.728226 miles6.0 miles is 9.65604 kilometers”

Result as expected

2.4 Enter number: 3(Centimeter/Inch)

Display “Enter number to be converted: ” Result as expected

2.4.1 Enter number to be converted: 28

Display “28.0 centimeters is 11.02363 inches28.0 inches is 71.12 centimeters”

Result as expected

3 Enter number: 2 Display “You chose option number 2 which is TEMPERATURE measurement”

Result as expected

3.1 Select preferred unit of conversion

Display Temperature Unit Menu Result as expected

3.2 Enter number: 1(Celsius/Fahrenheit)

Display “Enter number to be converted: ” Result as expected

14

Page 15: APLCassigmtdoc

3.2.1 Enter number to be converted: 11

Display “11.0 degrees Celsius is 51.8 degrees Fahrenheit11.0 degrees Fahrenheit is -11.66667 degrees Celsius”

Result as expected

3.3 Enter number: 2(Fahrenheit/Kelvin)

Display “Enter number to be converted: ” Result as expected

3.3.1 Enter number to be converted: 9

Display “9.0 degrees Fahrenheit is 260.3722 Kelvin9.0 Kelvin is -443.47 degrees Fahrenheit”

Result as expected

3.4 Enter number: 3(Kelvin/Celsius)

Display “Enter number to be converted: ” Result as expected

3.4.1 Enter number to be converted: 12

Display “12.0 Kelvin is -261.15 degrees Celsius12.0 degrees Celsius is 285.15 Kelvin”

Result as expected

4 Enter number: 3 Display “You chose option number 3 which is MASS measurement”

Result as expected

4.1 Select preferred unit of conversion

Display Mass Unit Menu Result as expected

4.2 Enter number: 1(Kilogram/Stone)

Display “Enter number to be converted: ” Result as expected

4.2.1 Enter number to be converted: 33

Display “33.0 kilograms is 5.196609 stones33.0 stones is 209.5596 kilograms”

Result as expected

4.3 Enter number: 2(Kilogram/Pound)

Display “Enter number to be converted: ” Result as expected

4.3.1 Enter number to be converted: 17

Display “17.0 kilograms is 37.47854 pounds17.0 pounds is 7.711064 kilograms”

Result as expected

4.4 Enter number: 3(Gram/Ounce)

Display “Enter number to be converted: ” Result as expected

4.4.1 Enter number to be converted: 45

Display “45.0 grams is 1.58733 ounces45.0 ounces is 1275.728 grams”

Result as expected

5 Enter number: 4 Display “You chose option number 3 which is TIME measurement”

Result as expected

5.1 Select preferred unit of conversion

Display Time Unit Menu Result as expected

5.2 Enter number: 1(Second/Minute)

Display “Enter number to be converted: ” Result as expected

5.2.1 Enter number to be converted: 101

Display “101.0 seconds is 1.68367 minutes101.0 minutes is 6060.0 seconds”

Result as expected

5.3 Enter number: 2(Minute/Hour)

Display “Enter number to be converted: ” Result as expected

5.3.1 Enter number to be converted: 27

Display “27.0 minutes is 0.45009 hours27.0 hours is 1620.0 minutes”

Result as expected

15

Page 16: APLCassigmtdoc

5.4 Enter number: 3(Second/Hour)

Display “Enter number to be converted: ” Result as expected

5.4.1 Enter number to be converted: 8

Display “8.0 seconds is 0.0022224 hours8.0 hours is 28800.0 seconds”

Result as expected

iii) Currency Conversion Menu

Case No.

Task Expected Result Actual Result

1 Select Malaysian Ringgit currency conversion

Display Currency Conversion Menu Result as expected

2 Enter number: 1 Display “You chose option number 1 which is US DOLLAR conversionEnter Malaysian Ringgit to be converted to US Dollar:”

Result as expected

2.1 Enter amount: 57 Display “57.0 Ringgit is 17.1228 US Dollars57.0 US Dollars is 189.696 Ringgit”

Result as expected

3 Enter number: 2 Display “You chose option number 2 which is EURO conversionEnter Malaysian Ringgit to be converted to Euro:”

Result as expected

3.1 Enter amount: 32 Display “32.0 Ringgit is 7.1776 Euros32.0 Euros is 142.6432 Ringgit”

Result as expected

4 Enter number: 3 Display “You chose option number 3 which is INDIAN RUPEE conversionEnter Malaysian Ringgit to be converted to Indian Rupee:”

Result as expected

4,1 Enter amount: 16 Display “16.0 Ringgit is 326.88 Rupees16.0 Rupees is 0.8 Ringgit”

Result as expected

5 Enter number: 4 Display “You chose option number 4 which is CHINESE YUAN conversionEnter Malaysian Ringgit to be converted to Chinese Yuan:”

Result as expected

5.1 Enter amount: 48 Display “48.0 Ringgit is 88.2768 Yuan48.0 Yuan is 26.0976 Ringgit”

Result as expected

6 Enter number: 5 Display “You chose option number 5 which is BRITISH POUND STERLING conversionEnter Malaysian Ringgit to be converted to British Pound Sterling:”

Result as expected

6.1 Enter amount: 64 Display “64.0 Ringgit is 12.3648 Pounds64.0 Pounds is 331.2128 Ringgit”

Result as expected

16

Page 17: APLCassigmtdoc

iv) Binary Conversion Menu

Case No.

Task Expected Result Actual Result

1 Select preferred binary conversion

Display Binary Conversion Menu Result as expected

2 Enter number: 1 Display “You chose option number 1 which is BINARY to DECIMAL conversionEnter a binary number:”

Result as expected

2.1 Enter binary number: 1011001

Display “"1011001" in decimal representation is 89”

Result as expected

3 Enter number: 2 Display “You chose option number 2 which is DECIMAL to BINARY conversionEnter the binary function (toBin), followed with a decimal number: ”

Result as expected

3.1 Enter binary function toBin with decimal number: 37

Display “"100101"” Result as expected

17

Page 18: APLCassigmtdoc

Prolog Application

Case No.

Query Expected Result Actual result

1 ?- father(X,Y). 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.

Result as expected

2 ?-mother(X,Y). 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.

Result as expected

3 ?- grandfather(X,Y). X = john,Y = kate ;X = john,Y = harry ;X = tom,Y = anna ;X = tom,Y = lisa ;X = john,Y = mike ;

Result as expected

18

Page 19: APLCassigmtdoc

Case No.

Query Expected Result Actual result

4 ?- grandmother(X,Y) X = celine,Y = kate ;X = celine,Y = harry ;X = lily,Y = anna ;X = lily,Y = lisa ;X = celine,Y = mike ;

Result as expected

5 ?- brother(X,Y). X = harry,Y = kate ;X = william,Y = lily ;X = william,Y = cindy ;

Result as expected

6 ?- sister(X,Y). 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 ;

Result as expected

7 ?-husband(X,Y). X = john,Y = celine ;X = paul,Y = kate ;X = tom,Y = lily ;X = william,Y = mary ;

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 ;

19

Page 20: APLCassigmtdoc

Case No.

Query Expected Result Actual result

8 ?-wife(X,Y). X = celine,Y = john ;X = lily,Y = tom ;X = mary,Y = william ;X = kate,Y = paul ;

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 ;

9 ?-uncle(X,Y). X = william,Y = kate ;X = harry,Y = anna ;X = harry,Y = lisa ;X = william,Y = harry ;

Result as expected

10 ?-aunt(X,Y). X = lily,Y = mike ;X = cindy,Y = mike ;X = cindy,Y = kate ;X = cindy,Y = harry ;

Result as expected

11 ?-fatherinlaw(X,Y). X = tom,Y = paul ;X = john,Y = tom ;X = john,Y = mary ;

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

20

Page 21: APLCassigmtdoc

Case No.

Query Expected Result Actual result

12 ?-motherinlaw(X,Y). X = lily,Y = paul ;X = celine,Y = tom ;X = celine,Y = mary ;

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

21

Page 22: APLCassigmtdoc

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

Page 23: APLCassigmtdoc

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

Page 24: APLCassigmtdoc

24

Page 25: APLCassigmtdoc

25

Page 26: APLCassigmtdoc

26

Page 27: APLCassigmtdoc

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

Page 28: APLCassigmtdoc

Prolog Application

i) Facts

ii) Rules

28

Page 29: APLCassigmtdoc

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

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.

29

Page 30: APLCassigmtdoc

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].

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

30