Haskell project
Programming and Paradigms, Dr. Pierpaolo Dondio
Due Date: 22 December 2017 (15% of total exam marks)
Define the following Haskell functions. Always include the function signature. (Note: the marks displayed sum up to 100)
1. is_square. [7]
Define a function is_square that takes an integer (positive) and returns True if the number is the square of an integer number. DO NOT use the SQRT function.
2. freq_letter_pc [7]
Define a function freq_letter_pc, that displays the percentage of each letter instead of the number of occurrences (do not count symbols other than [a..z]). Transform the text in small case first. Example:
freq_letter_pc “this is text” = [(t,0.3),(s,0.2),(i,0.2),(h,0.1),(e,0.1),(x,0.1)]
3. db in Haskell [14]
A small database contains two tables, one is a list of cities, identified by a city_id, city_name, city_population and country_ id. The other table is a list of countries (country_id, country_name). Table country is related by a 1 to many relation with table cities, country_id is the foreign key. The following is the content of the table:
cities
=[(1,”Paris”,7000000,1),(2,”London”,8000000,2),(1,”Rome”,3000000,3),
(1,”Edinburgh”,500000,2),(1,”Florence”,500000,3), (1,”Venice”,200000,3), (1,”Lyon”,1000000,1),(1,”Milan”,5000000,3), (1,”Madrid”,6000000,4),
(1,”Barcelona”,5000000,4),]
countries = [(1,”UK), (2,”France”), (3,”Italy”), (4,”Spain”)]
Write the following Haskell functions:
a. get_city_above n – to get all the names of the cities with a population above n (=input)
get_city_above 6000000 = [“Paris”,”London”,”Madrid”]
b. get_city country_name – to get all the cities given a country_name (not country_ id! You need to have a function that, given the country_name gets the country_id)
get_city “Italy” = [“Rome”,”Milan”,”Florence”,”Venice”]
c. num_city– to list all the country and for each country the number of cities in each country.
num_city = [(“UK”,2),(“Italy”,4), (“Fance”,2),(“Spain”,2)]
4. Euclidean distance between two lists [7]
Create a function eucl_dist to compute the euclidean distance between two vectors. Each vector is represented by a list of float number of unknown length. The two lists must have the same number of elements.
The distance between list x and y is defined as follows:
, = + +. . +
You can use the built-in Haskell function sqrt.
5. Language Identification [20]
The file lang.hs contains two lists of 26 float numbers each. The lists represent the frequency of each letter in English (list eng_freq) and Portuguese (pt_freq). For instance, the letter "b", the second letter in the alphabet, has a frequency of 1.492% in English and 1.04% in Portuguese.
Write a Haskell function get_lang that gets a text and print the message "The text is in English" if the text is written in English or "The text is in Portuguese" if the text is written in Portuguese (as a simplification, we only use text containing the 26 basic letters, without accents or other phonetic symbols). In order to identify the language, compute the frequency distribution of the letters in the text, store it into a list and check if the distribution is closer to eng_freq or pt_freq. You can use the function defined at exercise 2 and 6. Transform the text in small case first.
Using the function readfile, create a version of the program running from command line that takes a file as an input (command line parameter) and detect the language of the file.
HIGHER ORDER FUNCTIONS
6. Computing numerical series [17]
Write a higher order function math_series with two inputs: a function expressing a mathematical series (a function from a natural number to a float) and an integer positive number ,.
The function returns the sum of the first elements of the
CS案例:Haskell案例 Programming and Paradigms
2019-01-03