Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp:lab02 [2021/03/18 10:45] pdmatei |
fp:lab02 [2023/03/10 10:16] (current) pdmatei |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Introduction to Haskell ====== | + | ====== 2. Scala syntax and function definition ====== |
+ | ** Objectives: ** | ||
+ | * get yourself familiar with Scala syntax basics | ||
+ | * practice writing **tail-recursive** functions as an alternative to imperative **loops** | ||
+ | * keep your code clean and well-structured. | ||
- | ===== Functions in Haskell ===== | + | ** Create a new Scala worksheet to write your solutions ** |
- | In mathematics, functions have a domain an codomain. In Haskell, functions have **types** or **signatures**. They often can be omitted in Haskell, but can | ||
- | also be explicitly written as in: | ||
- | <code haskell> | + | **2.1.** Write a tail-recursive function that computes the factorial of a natural number. Start from the code stub below: |
- | f :: Integer -> Integer -> Integer | + | |
- | f x y = x + y | + | |
- | </code> | + | |
- | or: | + | <code scala> |
- | <code haskell> | + | def fact (n: Int): Int = { |
- | f :: Bool -> Bool | + | def aux_fact(i: Int, acc: Int): Int = |
- | f True = False | + | if (???) acc |
- | f False = True | + | else ??? |
+ | ??? | ||
+ | } | ||
</code> | </code> | ||
- | 3. Write a function together with its signature, which implements boolean AND: | + | **2.2.** Implement a tail-recursive function that computes the greatest common divisor of a natural number: |
- | <code haskell> | + | |
- | myand :: Bool -> Bool -> Bool | + | |
- | ... | + | |
- | </code> | + | |
- | 5. Write an implementation for a function ''ifp'' which takes a boolean, expressions $math[e_1] and $math[e_2] and returns $math[e_1] if the boolean is true and $math[e_2] otherwise. | + | <code scala> |
- | <code haskell> | + | def gcd(a: Int, b: Int): Int = ??? |
- | ifp = ... | + | |
</code> | </code> | ||
- | 6. Write a function which takes three integers and returns the largest. Hint - sometimes parentheses are **important in function calls**. | + | **2.3.** Write a tail-recursive function takes an integer $math[n] and computes the value $math[1 + 2^2 + 3^2 + ... + (n-1)^2 + n^2]. (Hint: use inner functions). |
- | <code haskell> | + | |
- | f :: Integer -> Integer -> Integer -> Integer | + | <code scala> |
+ | def sumSquares(n: Int): Int = ??? | ||
</code> | </code> | ||
- | <<We have to explain the syntax of if>> | + | ===== Newton's Square Root method ===== |
- | <<Explain syntax of guards>> | + | A very fast way to numerically compute $math[\sqrt{a}], often used as a standard //sqrt(.)// implementation, relies on Newton's Square Root approximation. The main idea relies on starting with an estimate (often 1), and incrementally improving the estimate. More precisely: |
+ | * Start with $math[x_0 = 1]. | ||
+ | * Compute $math[x_{n+1} = \displaystyle\frac{1}{2}(x_n+\frac{a}{x_n})] | ||
- | x. Solve the previous exercise using guards | + | **2.4.** Implement the function ''improve'' which takes an estimate $math[x_n] of $math[\sqrt{a}] and improves it (computes $math[x_{n+1}]). |
+ | <code scala> | ||
+ | def improve(xn: Double, a: Double): Double = ??? | ||
+ | </code> | ||
- | <<Explain lists, head, tail>> | + | **2.5.** Implement the function ''nthGuess'' which starts with $math[x_0 = 1] and computes the nth estimate $math[x_n] of $math[\sqrt{a}]: |
+ | <code scala> | ||
+ | def nth_guess(n: Int, a: Double): Double = ??? | ||
+ | </code> | ||
- | x. Implement reversal | + | Note that: |
+ | * for smaller $math[a], there is no need to compute $math[n] estimations as $math[(x_n)_n] converges quite fast to $math[\sqrt{a}]. | ||
+ | |||
+ | **2.6.** Thus, implement the function ''acceptable'' which returns ''true'' iff $math[\mid x_n^2 - a \mid \leq 0.001]. (Hint, google the ''abs'' function in Scala. Don't forget to import ''scala.math._''). | ||
+ | <code scala> | ||
+ | def acceptable(xn: Double, a: Double): Boolean = ??? | ||
+ | </code> | ||
- | 13. Write a function which extracts the third to last number from a list and returns ''True'', if that number is odd (hint: the function ''mod'' may be useful) | + | **2.7.** Implement the function ''mySqrt'' which computes the square root of an integer ''a''. Modify the previous implementations to fit the following code structure: |
- | <code> | + | <code scala> |
- | V | + | def mySqrt(a: Double): Double = { |
- | f [3,4,5,2,3,9] = False | + | def improve(xn: Double): Double = ??? |
- | f [3,4,2,1,4,4] = True | + | def acceptable(xn: Double): Boolean = ??? |
+ | |||
+ | def tailSqrt(estimate: Double): Double = ??? | ||
+ | |||
+ | ??? | ||
+ | } | ||
</code> | </code> | ||
- | <<Explain pattern matching>> | + | **2.8. (!) ** Try out your code for: ''2.0e50'' (which is $math[2.0\cdot 10^{50}]) or ''2.0e-50''. The code will likely take a very long time to finish. The reason is that $math[xn^2 - a] will suffer from rounding error which may be larger than 0.001. Can you find a different implementation for the function ''acceptable'' which takes that into account? (Hint: the code is just as simple as the original one). |
- | + | ||
- | x. Implement the previous exercise using patterns | + | |
- | + | ||
- | 14. Implement a function which returns the sum of integers from a list. | + | |
- | + | ||
- | 15. Implement a function which takes a list of booleans and returns false if **at least** one boolean from the list is false. | + | |
- | + | ||
- | 16. Implement a function which filters out all odd numbers from a list. | + | |
- | + | ||
- | 17. Implement a function which takes a list of booleans and returns a list of integers. In the latter, (''True'' becomes ''1'' and ''False'' becomes ''0''). Example: ''f [False, True, False] = [0,1,0]''. | + | |
- | + | ||
- | <<Explain char and strings>> | + | |
- | + | ||
- | x. Write a function which removes all empty strings from a list. | + | |
- | + | ||
- | x. Write a function which removes all strings of size smaller than 3. Do **not** use the builtin function ''length''. | + | |
- | + | ||
- | x. Write a function which removes all strings having the third letter equal to 'a'. | + | |
- | + | ||
- | x. Write a function which: | + | |
- | * removes all strings which are not **names**. A **name** always starts with an uppercase. | + | |
- | * removes the last name from the resulting list of names. A name always comprises of the first name followed by the last name. | + | |
- | + | ||