Difference between revisions of "FP Laboratory 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 141: Line 141:
 
max3 :: Int -> Int -> Int -> Int
 
max3 :: Int -> Int -> Int -> Int
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="Haskell">leapYear :: Int -> Bool</syntaxhighlight>
 
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
Line 160: Line 159:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
max2 :: Int -> Int -> Int
+
factorial  :: Int -> Int
max2 x y | x >= y = x
+
factorial  0 = 1
        |otherwise = y
+
factorial  n = n * factorial  (n-1)
 +
 
 +
combinations :: Int -> Int -> Int
 +
combinations n k = factorial n `div` (factorial k * factorial (n-k))
  
max3 :: Int -> Int -> Int -> Int
+
combinations' :: Int -> Int -> Int
max3 x y z = (x `max2` y) `max2` z
+
combinations' n k = fromIntegral(factorial n) `div` fromIntegral(factorial k * factorial (n-k))
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 178: Line 180:
 
         in a * a
 
         in a * a
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
<translate>
 
<translate>
 
<!--T:13-->
 
<!--T:13-->
Line 183: Line 193:
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
<translate>
 
<translate>
 
<!--T:14-->
 
<!--T:14-->
Line 188: Line 206:
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>

Revision as of 08:13, 24 September 2020

Types

  • Using the GHCi command :info, learn the type of the following functions (and operators): +, sqrt, succ, max
  • Get the information about the data type of following expressions and evaluate them. it is possible using the command :type. You can switch this option on for all commands by :set +t (removing by :unset +t).
5 + 8 
3 * 5 + 8
2 + 4
sqrt 16 
succ 6
succ 7
pred 9
pred 8
sin (pi / 2)
truncate pi
round 3.5
round 3.4 
floor 3.7 
ceiling 3.3
mod 10 3
odd 3
  • At presentations, we have spoken about some basic types: Int, Double, Bool, Char. For each of previous expressions assign them the most appropriate of these basic data types. You can verify your guess by using ::. For example, for the first expression, let's assume it is Int. We can cast the result to integer and get the following result.
Prelude> :type (5 + 8) :: Int
(5 + 8) :: Int :: Int

If we try incorrect conversion to Char, we get the following result.

Prelude> :type (5 + 8) :: Char

<interactive>:1:2: error:
    * No instance for (Num Char) arising from a use of `+'
    * In the expression: (5 + 8) :: Char

For this expression, also the type Double works.

Prelude> :type (5 + 8) :: Double
(5 + 8) :: Double :: Double

Reasoning about types

For following expression, try to determine:

  • if the expression's type is correct;
  • what will be the type of the result;
  • what will be the result;
  • put the expression into the interpreter, and verify your claims.
5.9/7
(floor 5.9)/7
floor 5.9/7
fromIntegral floor 5.9/7
fromIntegral (floor 5.9)/7
div (floor 5.9) 7
(floor 5.9) div 7
(floor 5.9) `div` 7
mod 10/2 3
mod (floor (10/2)) 3

Simple functions

Implement following functions:

  • Function that computes a factorial of a given number.
factorial :: Int -> Int
factorial  :: Int -> Int
factorial  0 = 1
factorial  n = n * factorial  (n-1)

factorial'  :: Int -> Int
factorial' n | n==0 = 1
             | otherwise = n * factorial'' (n-1)

factorial''  :: Int -> Int        
factorial'' n = if n==0 then 1 else n * factorial'' (n-1)
  • Function that computes n-th number in Fibonacci sequence.
fib :: Int -> Int
fib :: Int->Int
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

fib' :: Int -> Int
fib' n = tmp n 1 1 where
  tmp 0 a _ = a 
  tmp x a b = tmp (x-1) b (a+b)
  • Function that checks if a year is a leap-year (divisible without remainder by 4 and it is not divisible by 100. If it is divisible by 400, it is a leap-year).
leapYear :: Int -> Bool
leapYear :: Int -> Bool
leapYear x = x `mod` 4 == 0 && x `mod` 100 /= 0 || x `mod` 400 == 0

leapYear' :: Int -> Bool
leapYear' x | x `mod` 400 == 0 = True
            | x `mod` 100 /= 0 = False
            | otherwise = x `mod` 4 == 0
  • Implement two functions that returns a maximum from 2 respectively 3 given parameters.
max2 :: Int -> Int -> Int
max3 :: Int -> Int -> Int -> Int
max2 :: Int -> Int -> Int
max2 x y | x >= y = x
         |otherwise = y

max3 :: Int -> Int -> Int -> Int
max3 x y z = (x `max2` y) `max2` z
  • Term combination is a selection of items from a collection, such that (unlike permutations) the order of elements in this selection does not matter. Compute the number of possible combinations if we are taking k things from the collection of n things.
combinations :: Int -> Int -> Int
factorial  :: Int -> Int
factorial  0 = 1
factorial  n = n * factorial  (n-1)

combinations :: Int -> Int -> Int
combinations n k = factorial n `div` (factorial k * factorial (n-k))

combinations' :: Int -> Int -> Int
combinations' n k = fromIntegral(factorial n) `div` fromIntegral(factorial k * factorial (n-k))
  • Implement a function that computes the number of solutions for a quadratic equation. This quadratic equation will be given using standard coefficients: a, b, c.
numberOfRoots :: Int -> Int -> Int -> Int
-- To simplify the solution, let construct can be used
f x y = let a = x + y
        in a * a
  • Implement a function that computes greatest common divider for two given numbers.
gcd' :: Int -> Int -> Int
  • Implement a function that compute, if a given number is a prime number.
isPrime :: Int -> Bool