Exercise 1: A few of Chemistry.
Put into a list, called alkaline_earth_metals, the atomic numbers of the six alkaline earth metals: beryllium (4), magnesium (12), calcium (20), strontium (38), barium (56), and radium (88). Then
- Write a function that returns the highest atomic number in alkaline_earth_metals.
- Write a function that sorts alkaline_earth_metals in ascending order (from the lightest to the heaviest).
Put into a second list, called noble_gases, the noble gases: helium (2), neon (10), argon (18), krypton (36), xenon (54), and radon (86). Then
- Write a function (or a group of functions) that merges the two lists and print the result as couples (name, atomic number) sorted in ascending order on the element names.
Exercise 2: Temperature Conversion System.
Beyond the well-known Celsius and Fahrenheit, there are other six temperature scales: Kelvin, Rankine, Delisle, Newton, Réaumur, and Rømer (Look at http://en.wikipedia.org/wiki/Comparison_of_temperature_scales to read about them).
- Write a function that given a pure number prints a conversion table for it among any of the 8 scales (remember that functions are high-order).
- Write a function that given a temperature in a specified scale returns a list of all the corresponding temperatures in the other scales, note that the scale must be specified (hint: use a tuple).
Exercise 3: Matrix Calculi.
Write the matrix datatype with the following operations:
- A function zeroes to construct a matrix of size n×m filled with zeros.
- A function identity to construct the identity matrix (the one with all 0s but the 1s on the diagonal) of given size.
- A function init to construct a square matrix of a given size n filled with the first n×n integers.
- A function transpose that transposes a generic matrix independently of its size and content.
- A function * that multiplies two matrices non necessarily square matrices.
Exercise 4: Frequencies.
Let's write a function (or a pool of functions) that given a quite large text (over than 2000 words) counts how frequent each word occurs in the text.
The text is read from a file (look at the pervasive module in the manual) and it is a real text with punctuation (i.e., commas, semicolons, ...) that should be counted.
Note that words with different case should be considered the same.
Exercise 5: Playing around with Strings.
Define the following functions/operators on strings:
- is_palindrome: string → bool that checks if the string is palindrome, a string is palindrome when the represented sentence can be read the same way in either directions in spite of spaces, punctual and letter cases, e.g., detartrated, "Do geese see God?", "Rise to vote, sir.", ...
- operator (-): string → string → string that subtracts the letters in a string from the letters in another string, e.g., "Walter Cazzola"-"abcwxyz" will give "Wlter Col" note that the operator - is case sensitive
- anagram : string → string list → boolean that given a dictionary of strings, checks if the input string is an anagram of one or more of the strings in the dictionary
Exercise 6: Approximated Trigonometric Functions.
sin(x) can be approximate by the Taylor's series:
Similarly you can approximate all the trigonometric and transcendent functions (look at http://en.wikipedia.org/wiki/Taylor_series).
Let's write a module to implement sin x n by using the Taylor's series (where n is the level of approximation, i.e., 1 only one item, 2 two items, 3 three items and so on). Do the same with cosine, tangent, logarithm and so on.
Let's compare your functions with those implemented in the pervasive module at the growing of the approximation level.
Exercise 7: Playing around with Arithmetic (the Polish Calculator).
Write a PolishCalculator module that implements a stack-based calculator that adopts polish notation for the expressions to be evaluated.
Polish Notation is a prefix notation wherein every operator follows all of its operands; this notation has the big advantage of being unambiguous and permits to avoid the use of parenthesis. E.g., (3+4)*5 is equal to 3 4 + 5 *.
The module should include an:
- Expr datatype representing an expression
- a function expr_of+string: string → Expr which build the expression in the corresponding infix notation out of the string in polish notation;
- a function``eval: Expr → int`` which will evaluate the expression and returns such evaluation
The recognized operators should be +, - (both unary and binary), *, /, ** over integers. At least a space ends each operands and operators.
The evaluation/translation can be realized by pushing the recognized elements on a stack. Define the module independently of the Stack implementation and try to use functors to adapt it.