Difference between revisions of "FP Laboratory 3/cs"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 213: Line 213:
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
<div class="mw-translate-fuzzy">
 
 
* Vytvořte funkci která vrátí skalární součet dvou vektorů..
 
* Vytvořte funkci která vrátí skalární součet dvou vektorů..
</div>
 
  
 
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>

Revision as of 09:47, 27 September 2023

Použití seznamů

Zjistěte jaké jsou výsledky následujících operací:

[3,2,1] > [2,1,0]  
[3,2,1] > [2,10,100]  
[3,4,2] > [3,4]  
[3,4,2] > [2,4]  
[3,4,2] == [3,4,2]

Jednoduché funkce pracující se seznamy

Implementujte následující funkce:

  • Vytvořte funkci, která vypočítá délku seznamu.
length' :: [a] -> Int
*Main> length' "ABCD"
4
length' :: [a] -> Int
length' []  = 0
length' (_:xs) = 1 + length' xs
Try it!
  • Vytvořte funkci, která sečte seznam celých čísel.
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!
  • Vytvořte funkci která vrátí první prvek v seznamu.
getHead :: [a] -> a
*Main> getHead [1,2,3]
1
getHead :: [a] -> a
getHead (x:_) = x
Try it!
  • Vytvořte funkce která vrátí poslední prvek v seznamu.
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!
  • Vytvořte funkci která ověří, zdali je daný prvek obsažen v seznamu.
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!
  • Vytvořte funkci která vrátí seznam bez prvního prvku.
getTail :: [a] -> [a]
*Main> getTail [1,2,3]
[2,3]
getTail :: [a] -> [a]                   
getTail (_:xs) = xs
Try it!
  • CVytvořte funkci která vátí seznam bez posledního prvku.
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!
  • Vytvořte funkci, která sloučí dva seznamy do jednoho seznamu.
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!
  • Vytvořte funkci, která najde maximum v seznamu celých čísel.
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!
  • Vytvořte funkci, která obrátí seznam.
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!
  • Vytvořte funkci která vrátí skalární součet dvou vektorů..
scalar :: [Int] -> [Int] -> Int
*Main> scalar [1,2,3] [4,5,6]
32
scalar :: [Int] -> [Int] -> Int
scalar [] [] = 0
scalar (x:xs) (y:ys) = x*y + scalar xs ys
Try it!

Additional exercises

  • Create a function that eliminates all occurrences of zeros in a list.
nonZeros :: [Int] -> [Int]
*Main> nonZeros [0,1,0,2,3,0,0]
[1,2,3]
  • Create a function that realizes the left rotation of a list by one element.
rotateLeft1 :: [a] -> [a]
*Main> rotateLeft1 [1,2,3,4,5]
[2,3,4,5,1]
  • Create a function that realizes the right rotation of a list by one element.
rotateRight1 :: [a] -> [a]
*Main> rotateRight1 [1,2,3,4,5]
[5,1,2,3,4]
  • Create a function that eliminates all even numbers from a list.
oddMembers :: [Int] -> [Int]
*Main> oddMembers [0,1,0,2,3,0,0] 
[1,3]
  • Create a function that counts all odd numbers in a given list (define non-recursively using previously defined functions).
countOddMembers :: [Int] -> Int
*Main> countOddMembers [1,0,3,1,4,5]
4
  • Create a function that compares lists of numbers for equality.
compareLists :: [a] -> [a] -> Bool
*Main> compareLists [5,8,11] [5,8,11]
True
*Main> compareLists [5,8,11] [3,5,8]
False
*Main> compareLists [] []
True
*Main> compareLists [5,8,11] [5,8,11,13]
False