Difference between revisions of "PFP Laboratory 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "== Simple functions working with list == Implement following functions: * Create a function that computes length of a list. <syntaxhighlight lang="Haskell">length' :: [a] -> I...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
== More Functions ==
 +
Implement following functions:
 +
* 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.
 +
<syntaxhighlight lang="Haskell">combinations :: Int -> Int -> Int</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> combinations 8 5
 +
56
 +
</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
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))
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 +
* 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.
 +
<syntaxhighlight lang="Haskell">numberOfRoots :: Int -> Int -> Int -> Int
 +
-- To simplify the solution, let construct can be used
 +
f x y = let a = x + y
 +
        in a * a
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> numberOfRoots 1 4 2
 +
2
 +
</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
numberOfRoots :: Int -> Int -> Int -> Int
 +
numberOfRoots a b c = let d = b*b - 4 * a *c
 +
                      in if d<0 then 0 else if d == 0 then 1 else 2
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 +
* Implement a function that computes greatest common divider for two given numbers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/uNs9Zqetu7k]]</div>
 +
<syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> gcd' 30 18
 +
6
 +
</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
gcd' :: Int -> Int -> Int
 +
gcd' a b | a > b = gcd' (a-b) b
 +
        | a < b = gcd' a (b-a)
 +
        | a==b = a
 +
 +
gcd2 :: Int -> Int -> Int       
 +
gcd2 a 0 = a
 +
gcd2 a b = gcd2 b (a `mod` b)
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]]
 +
</div>
 +
<div style="clear:both"></div>
 +
* Implement a function that compute, if a given number is a prime number. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/XSc8qjd4StQ]]</div>
 +
<syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> isPrime 7
 +
True
 +
</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
isPrime :: Int -> Bool
 +
isPrime 1 = False
 +
isPrime y = isPrimeTest y (y-1) where
 +
  isPrimeTest _ 1 = True
 +
  isPrimeTest n x | n `mod` x ==0 = False
 +
                  | otherwise = isPrimeTest n (x-1)
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 
== Simple functions working with list ==
 
== Simple functions working with list ==
 
Implement following functions:
 
Implement following functions:
Line 91: Line 178:
 
isElement a (x:xs) | a == x = True  
 
isElement a (x:xs) | a == x = True  
 
                   | otherwise = isElement a xs  
 
                   | otherwise = isElement a xs  
</syntaxhighlight>
 
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
<div style="clear:both"></div>
 
 
* Create a function that returns the list without the first element.
 
 
<syntaxhighlight lang="Haskell">getTail :: [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
*Main> getTail [1,2,3]
 
[2,3]
 
</syntaxhighlight>
 
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
getTail :: [a] -> [a]                 
 
getTail (_:xs) = xs
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
Line 201: Line 271:
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
* Create a function that product scalar multiplication if two vectors.
+
== Advanced functions working with lists == <!--T:1-->
 +
Implement following functions:
 +
 
 +
* Create a function that takes first n elements of the list.
  
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>
+
<syntaxhighlight lang="Haskell">take' :: Int -> [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
*Main> scalar [1,2,3] [4,5,6]
+
*Main> take' 2 [1,2,3]
32
+
[1,2]
 
</syntaxhighlight>
 
</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">
scalar :: [Int] -> [Int] -> Int
+
take' :: Int -> [a] -> [a]
scalar [] [] = 0
+
take' 0 _ = []
scalar (x:xs) (y:ys) = x*y + scalar xs ys
+
take' _ [] = []
 +
take' n (x:xs) = x: take' (n-1) xs
 
</syntaxhighlight>
 
</syntaxhighlight>
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
+
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 
 +
* Create a function that takes the remaining list after the first n elements.
 +
 
 +
<syntaxhighlight lang="Haskell">drop' :: Int -> [a] -> [a]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> drop' 2 [1,2,3]
 +
[3]
 +
</syntaxhighlight>
 +
 
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
drop' :: Int -> [a] -> [a]
 +
drop' 0 x = x
 +
drop' _ [] = []
 +
drop' n (_:xs) = drop' (n-1) xs
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 
 +
* Create a function that find the smallest element in the list. Consider input restrictions.
 +
 
 +
<syntaxhighlight lang="Haskell">minimum' :: [a] -> a -- Is this right?</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
 
 +
*Main> minimum' [1,3,4,0]
 +
0
 +
</syntaxhighlight>
 +
 
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
minimum' :: Ord a => [a] -> a
 +
minimum' [x] = x
 +
minimum' (x:y:z) | x < y = minimum' (x:z)
 +
                | otherwise = minimum' (y:z)
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]]
 +
</div>
 +
<div style="clear:both"></div>
 +
 
 +
* Find all integer divisors of a given number.
 +
 
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/8iKGkcOlzpI]]</div>
 +
<syntaxhighlight lang="Haskell">divisors :: Int -> [Int]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> divisors 32 
 +
[1,2,4,8,16,32]
 +
</syntaxhighlight>
 +
 
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
divisors :: Int -> [Int]
 +
divisors n = tmp n where
 +
  tmp 0 = []
 +
  tmp x | n `mod` x == 0 = x: tmp (x-1)
 +
        | otherwise = tmp (x-1)
 +
 
 +
divisors' :: Int -> [Int]
 +
divisors' n =  filter (\x -> n `mod` x == 0) [1..n]
 +
 
 +
divisors'' :: Int -> [Int]
 +
divisors'' n =  [x | x<-[1..n], n `mod` x == 0]
 +
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>

Latest revision as of 10:51, 29 September 2022

More Functions

Implement following functions:

  • 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
*Main> combinations 8 5
56
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))
Try it!
  • 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
*Main> numberOfRoots 1 4 2
2
numberOfRoots :: Int -> Int -> Int -> Int
numberOfRoots a b c = let d = b*b - 4 * a *c
                      in if d<0 then 0 else if d == 0 then 1 else 2
Try it!
  • Implement a function that computes greatest common divider for two given numbers.
    Video logo.png
gcd' :: Int -> Int -> Int
*Main> gcd' 30 18
6
gcd' :: Int -> Int -> Int
gcd' a b | a > b = gcd' (a-b) b
         | a < b = gcd' a (b-a)
         | a==b = a

gcd2 :: Int -> Int -> Int         
gcd2 a 0 = a
gcd2 a b = gcd2 b (a `mod` b)
Try it!
  • Implement a function that compute, if a given number is a prime number.
    Video logo.png
isPrime :: Int -> Bool
*Main> isPrime 7
True
isPrime :: Int -> Bool
isPrime 1 = False
isPrime y = isPrimeTest y (y-1) where
  isPrimeTest _ 1 = True 
  isPrimeTest n x | n `mod` x ==0 = False
                  | otherwise = isPrimeTest n (x-1)
Try it!

Simple functions working with list

Implement following functions:

  • Create a function that computes length of a list.
length' :: [a] -> Int
*Main> length' "ABCD"
4
length' :: [a] -> Int
length' []  = 0
length' (_:xs) = 1 + length' xs
Try it!
  • Create a function that sums the list of integers.
Video logo.png
sumIt :: [Int] -> Int
*Main> sumIt [1,2,3]
6
sumIt :: [Int] -> Int
sumIt []  = 0
sumIt (x:xs) = x + sumIt xs
Try it!
  • Create a function that returns the first element in the list.
getHead :: [a] -> a
*Main> getHead [1,2,3]
1
getHead :: [a] -> a
getHead (x:_) = x
Try it!
  • Create a function that returns the last element in the list.
Video logo.png
getLast :: [a] -> a
*Main> getLast [1,2,3]
3
getLast :: [a] -> a
getLast [x] = x
getLast (x:xs) = getLast xs

getLast' :: [a] -> a
getLast' (x:xs) | length xs == 0 = x
                | otherwise = getLast' xs
Try it!
  • Create a function that checks if an element is a member of the list.
isElement :: Eq a => a -> [a] -> Bool
*Main> isElement 2 [1,2,3]
True
isElement :: Eq a => a -> [a] -> Bool
isElement _ [] = False
isElement a (x:xs) | a == x = True 
                   | otherwise = isElement a xs
Try it!
  • Create a function that returns the list without the last element.
Video logo.png
getInit :: [a] -> [a]
*Main> getInit [1,2,3]
[1,2]
getInit :: [a] -> [a]
getInit [_] = []
getInit (x:xs) = x : getInit xs
Try it!
  • Create a function that merge two lists into one list.
Video logo.png
combine :: [a] -> [a] -> [a]
*Main> combine [1,2,3] [4,5]
[1,2,3,4,5]
combine :: [a] -> [a] -> [a]
combine [] y = y
combine (x:xs) y = x : combine xs y
Try it!
  • Create a function that finds the maximum in the list of integers.
Video logo.png
max' :: [Int] -> Int
*Main> max' [3,1,7,5]
7
max' :: [Int] -> Int
max' [x] = x
max' (x:y:z) | x > y = max' (x:z)
             | otherwise = max' (y:z)

max'' :: [Int] -> Int
max'' (y:ys) = tmp y ys where
   tmp a [] = a
   tmp a (x:xs) | x > a = tmp x xs 
                |otherwise = tmp a xs
Try it!
  • Create a function that reverse a list.
Video logo.png
reverse' :: [a] -> [a]
*Main> reverse' [3,1,7,5]
[5,7,1,3]
reverse' :: [a] -> [a]             
reverse' [] = []
reverse' (x:xs) = (reverse' xs) ++ [x]

reverse'' :: [a] -> [a] 
reverse'' n = tmp n  []
  where tmp [] ys = ys 
        tmp (x:xs) ys = tmp xs (x:ys)
Try it!

Advanced functions working with lists

Implement following functions:

  • Create a function that takes first n elements of the list.
take' :: Int -> [a] -> [a]
*Main> take' 2 [1,2,3]
[1,2]
take' :: Int -> [a] -> [a]
take' 0 _ = []
take' _ [] = []
take' n (x:xs) = x: take' (n-1) xs
Try it!
  • Create a function that takes the remaining list after the first n elements.
drop' :: Int -> [a] -> [a]
*Main> drop' 2 [1,2,3]
[3]
drop' :: Int -> [a] -> [a]
drop' 0 x = x
drop' _ [] = []
drop' n (_:xs) = drop' (n-1) xs
Try it!
  • Create a function that find the smallest element in the list. Consider input restrictions.
minimum' :: [a] -> a -- Is this right?
*Main> minimum' [1,3,4,0]
0
minimum' :: Ord a => [a] -> a 
minimum' [x] = x
minimum' (x:y:z) | x < y = minimum' (x:z)
                 | otherwise = minimum' (y:z)
Try it!
  • Find all integer divisors of a given number.
Video logo.png
divisors :: Int -> [Int]
*Main> divisors 32  
[1,2,4,8,16,32]
divisors :: Int -> [Int]
divisors n = tmp n where
  tmp 0 = []
  tmp x | n `mod` x == 0 = x: tmp (x-1)
        | otherwise = tmp (x-1)

divisors' :: Int -> [Int]
divisors' n =  filter (\x -> n `mod` x == 0) [1..n] 

divisors'' :: Int -> [Int]
divisors'' n =  [x | x<-[1..n], n `mod` x == 0]
Try it!