The objective of this assignment is to research how generic programing works in different programing languages.
The general idea of generics (or polymorphism as it is sometimes called in a functional programing context) is to reuse the same source code with different types.
Homework 2
- Read about “Generic programming”:
Start with https://en.wikipedia.org/wiki/Generic_programming
- Review Lab Assignment 1. Remember that the sort functions in C and Haskell had different types: In C we sorted arrays of integers, in Haskell we sorted lists of arbitrary elements that had some notion of order. Why do you think did we restrict the type of the C sort functions in this way?
Lab Assignment 2
- In this lab, you will use 5 different programming languages to solve the same problem: Use the same generic sort function to sort values of different types. The values to sort could be integers, floating point numbers, strings, pairs of values (say a string and a number), etc.
- Write a console application in each of these 5 languages: C, C++, C#, Python and Haskell.
- Each application has to use the following data:
The sequence of floating point numbers:
645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26
The following sequence of people with name and age of each person. The name is a string and the age an integer:
Hal, 20; Susann, 31; Dwight, 19; Kassandra, 21; Lawrence, 25; Cindy, 22; Cory, 27;
Mac, 19; Romana, 27; Doretha, 32; Danna, 20; Zara, 23; Rosalyn, 26; Risa, 24; Benny, 28; Juan, 33; Natalie, 25
Use appropriate data structures to represent the data above in each of the 5 languages and define the variables numbers and people, respectively. 4. Find generic sort functions for each of the 5 languages.
1 of 2
- The objective of this assignment is to understand generics (not sorting). You can use the sort functions from Lab Assignment 1 or just use a sort function provided in some standard library for the respective language.
- C doesn’t really provide generics. However, a void* can be used to point to any value.
- One way to specify an order on a type is to define a comparison function that compares two values. This comparison function could be an argument to your sort function. Some languages might provide predefined comparison functions.
- Try to use everything we learned about these different programing languages, e.g., Python uses duck-typing, Haskell uses the type-class Ord to express order on a type, LINQ in C# includes the orderby operator, etc.
- Use the generic sort functions of each language to
- sort numbers ascending by numerical value,
- sort people alphabetically (lexicographically) by name, and to
- sort people descending by age, where people of the same age should be sorted alphabetically (lexicographically).
- The point here is to reuse as much code as possible to perform all 3 different sort operations. Try to isolate the specific code that is needed for each of the three tasks.
- Write a main function in each of the 5 languages to test your code by writing the sort results to the console.
Deliverable
- You can work on this assignment in a group of up to 6 students.
- At the due date you will take a brief quiz to test your understanding of the assignment.
- During the lab session on the due date each group will do
- A brief demonstration of the running applications.
- A presentation explaining the source code.
Reviews
There are no reviews yet.