Difference between revisions of "FP Laboratory 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Marked this version for translation)
Line 1: Line 1:
 
<translate>
 
<translate>
== Types ==
+
== Types == <!--T:1-->
  
 +
<!--T:2-->
 
*Using the GHCi command <code>:info</code>, learn the type of the following functions (and operators): <code>+, sqrt, succ, max</code>
 
*Using the GHCi command <code>:info</code>, learn the type of the following functions (and operators): <code>+, sqrt, succ, max</code>
 
*Get the information about the data type of following expressions and evaluate them. it is possible using the command <code>:type</code>. You can switch this option on for all commands by <code>:set +t</code> (removing by <code>:unset +t</code>).   
 
*Get the information about the data type of following expressions and evaluate them. it is possible using the command <code>:type</code>. You can switch this option on for all commands by <code>:set +t</code> (removing by <code>:unset +t</code>).   
Line 24: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:3-->
 
* At presentations, we have spoken about some basic types: <code> Int, Double, Bool, Char</code>. For each of previous expressions assign them the most appropriate of these basic data types. You can verify your guess by using <code>::</code>. For example, for the first expression, let's assume it is <code>Int</code>. We can cast the result to integer and get the following result.
 
* At presentations, we have spoken about some basic types: <code> Int, Double, Bool, Char</code>. For each of previous expressions assign them the most appropriate of these basic data types. You can verify your guess by using <code>::</code>. For example, for the first expression, let's assume it is <code>Int</code>. We can cast the result to integer and get the following result.
 
</translate>
 
</translate>
Line 31: Line 33:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:4-->
 
If we try incorrect conversion to <code>Char</code>, we get the following result.
 
If we try incorrect conversion to <code>Char</code>, we get the following result.
 
</translate>
 
</translate>
Line 41: Line 44:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:5-->
 
For this expression, also the type <code>Double</code> works.
 
For this expression, also the type <code>Double</code> works.
 
</translate>
 
</translate>
Line 48: Line 52:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
== Simple functions ==
+
== Simple functions == <!--T:6-->
  
 +
<!--T:7-->
 
Implement following functions:
 
Implement following functions:
 
* Function that computes a factorial of a given number.
 
* Function that computes a factorial of a given number.
Line 55: Line 60:
 
<syntaxhighlight lang="Haskell">factorial :: Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">factorial :: Int -> Int</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:8-->
 
* Function that computes n-th number in Fibonacci sequence.
 
* Function that computes n-th number in Fibonacci sequence.
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">fib :: Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">fib :: Int -> Int</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:9-->
 
* 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).
 
* 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).
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">leapYear :: Int -> Bool</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">leapYear :: Int -> Bool</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:10-->
 
* Implement two functions that returns a maximum from 2 respectively 3 given parameters.
 
* Implement two functions that returns a maximum from 2 respectively 3 given parameters.
 
</translate>
 
</translate>
Line 70: Line 78:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:11-->
 
* 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.  
 
* 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.  
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">combinations :: Int -> Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">combinations :: Int -> Int -> Int</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:12-->
 
* 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.
 
* 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.
 
</translate>
 
</translate>
Line 82: Line 92:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:13-->
 
* Implement a function that computes greatest common divider for two given numbers.
 
* Implement a function that computes greatest common divider for two given numbers.
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight>
 
<translate>
 
<translate>
 +
<!--T:14-->
 
* Implement a function that compute, if a given number is a prime number.
 
* Implement a function that compute, if a given number is a prime number.
 
</translate>
 
</translate>
 
<syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight>

Revision as of 08:20, 10 October 2019

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

Simple functions

Implement following functions:

  • Function that computes a factorial of a given number.
factorial :: Int -> Int
  • Function that computes n-th number in Fibonacci sequence.
fib :: Int -> Int
  • 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
  • Implement two functions that returns a maximum from 2 respectively 3 given parameters.
max2 :: Int -> Int -> Int
max3 :: Int -> Int -> Int -> Int
  • 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
  • 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