Esercizi di Programmazione Avanzata

Exercises on Functional Programming and Other Silly Things


Exercise 1: A Few of Math with Lists.

Write the solutions for the following quizzes by using functional programming:

  1. Sum all the natural numbers below one thousand that are multiples of 3 or 5.
  2. Calculate the smallest number divisible by each of the numbers 1 to 20.
  3. Calculate the sum of the figures of 2^1000
  4. Calculate the first term in the Fibonacci sequence to contain 1000 digits.
Exercise 2: Frequencies.

Let's write a module (a pool of functions) that given a quite large text (over than 2000 words) counts how frequent each word occurs in the text. In particular the module should provide the function freqs that given a filename and a number would return a list of words (with their frequencies) that occur more than the given number; the list is sorted by frequency with the higher first.

The text is read from a file and it is a real text with punctuation (i.e., commas, semicolons, ...) that shouldn't be counted.

Note that words that differ only for the case should be considered the same.

Exercise 3: Approximated Trigonometric Functions.

sin(x) can be approximate by the Taylor's series:

x - x^3/3! + x^5/5! ...

Let's write a library 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).

Let's compare your function with the one implemented in the math module at the growing of the approximation level.

Hint. Use a generator for the factorial and a comprehension for the series.

Walter Cazzola

Didactics

Publications

Funded Projects

Research Projects

Related Events








Valid XHTML 1.0 Transitional