Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fp:lab02 [2021/03/18 12:24]
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:
-:: Integer -> Integer -> Integer +
-f x y = x + y +
-</​code>​+
  
-or: +<​code ​scala
-<​code ​haskell+def fact (nInt)Int { 
-:: Bool -> Bool +   def aux_fact(i: Int, acc: Int): Int  
-f True False +       if (???) acc 
-f False True+       else ??? 
 +   ??? 
 +}
 </​code>​ </​code>​
  
-The previous example illustrates ​that we can define functions by specifying ​behaviour for given values.+**2.2.** Implement a tail-recursive function ​that computes the greatest common divisor of natural number:
  
-3.  Write a function together with its signature, which implements boolean AND: +<​code ​scala
-<​code ​haskell+def gcd(aInt, bInt): Int = ???
-myand :: Bool -> Bool -> Bool +
-...+
 </​code>​ </​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. +**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>​ +
-ifp = ... +
-</​code>​+
  
-6. Write a function which takes three integers and returns the largest. Hint - sometimes parentheses are **important in function calls**. +<​code ​scala
-<​code ​haskell+def sumSquares(nInt)Int = ???
-:: Integer -> Integer -> Integer -> Integer+
 </​code>​ </​code>​
  
-In Haskell, we can use the if constructin manner almost identical to the above implementation, ​e.g+===== Newton'​s Square Root method ===== 
-<​code ​haskell+ 
-f x = if x == 0 then 1 else 0+A very fast way to numerically compute $math[\sqrt{a}]often used as standard //​sqrt(.)// ​implementation, ​relies on Newton'​s Square Root approximationThe 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})] 
 + 
 +**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>​ </​code>​
  
-The previous function returns 1 if x is equal to 0 and 0 otherwise. A more elegant way is to use **guards**: +**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 ​haskell+<​code ​scala
-f x +def nth_guess(n:​ Int, a: Double): Double ​???
-   | x == 0 = 1 +
-   | otherwise ​0+
 </​code>​ </​code>​
  
-More generally, guards can be used as follows: +Note that
-<code haskell>​ +  ​* 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}]. ​ 
-<​function_name>​ <​parameters>​ +  
-  ​| <​boolean_condition_1>​ = <​expression_1>​ +**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._''​). 
-  | <​boolean_condition_2>​ = <​expression_2>​ +<code scala> 
-  ... +  ​def acceptable(xn:​ Double, a: Double): Boolean ​???
-  ​| otherwise ​<​expression_n>​+
 </​code>​ </​code>​
  
-xSolve the previous exercise using guards +**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 ​scala
-<<​Explain lists, head, tail>>​ +def mySqrt(a: Double): Double = { 
- +   def improve(xn: Double): Double ​??? 
-x. Implement ​reversal +   def acceptable(xn:​ Double): Boolean ​??? 
- +    
-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) +   def tailSqrt(estimate:​ Double): Double = ??? 
-<​code>​ +    
-            V +   ??? 
-   f [3,​4,​5,​2,​3,​9] ​False +}
-   f [3,​4,​2,​1,​4,​4] ​True+
 </​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 very long time to finishThe 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)
- +
-xImplement the previous exercise using patterns +
- +
-14Implement 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: ​''​[False, True, False] = [0,1,0]''​. +
- +
-<<​Explain char and strings>>​ +
- +
-xWrite function which removes all empty strings from a list. +
- +
-x. Write function ​which removes all strings of size smaller ​than 3Do **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. +
- +