Difference between revisions of "FP Laboratory 3"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Usage of lists ==
+
<translate>
 +
== Usage of lists == <!--T:1-->
 +
Find out the results of the following operations:
 +
</translate>
  
Find out the results of the following operations:
 
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
[3,2,1] > [2,1,0]   
 
[3,2,1] > [2,1,0]   
Line 10: Line 12:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Simple functions working with list ==
+
<translate>
 +
== Simple functions working with list == <!--T:2-->
 
Implement following functions:
 
Implement following functions:
 
* Create a function that computes length of a list.
 
* Create a function that computes length of a list.
 +
</translate>
 +
 
<syntaxhighlight lang="Haskell">length' :: [a] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">length' :: [a] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 25: Line 30:
 
length' (_:xs) = 1 + length' xs
 
length' (_:xs) = 1 + length' xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
* Create a function that sums the list of integers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/Yuz8lpKZiJs]]</div>
+
<translate>
 +
<!--T:3-->
 +
* Create a function that sums the list of integers.
 +
</translate>
 +
 
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/Yuz8lpKZiJs]]</div>
 
<syntaxhighlight lang="Haskell">sumIt :: [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">sumIt :: [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 41: Line 52:
 
sumIt (x:xs) = x + sumIt xs
 
sumIt (x:xs) = x + sumIt xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:4-->
 
* Create a function that returns the first element in the list.
 
* Create a function that returns the first element in the list.
 +
</translate>
 +
 
<syntaxhighlight lang="Haskell">getHead :: [a] -> a</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell">getHead :: [a] -> a</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 56: Line 72:
 
getHead (x:_) = x
 
getHead (x:_) = x
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
* Create a function that returns the last element in the list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/I5AZu8_G8pQ]]</div>
+
<translate>
 +
<!--T:5-->
 +
* Create a function that returns the last element in the list.
 +
</translate>
 +
 
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/I5AZu8_G8pQ]]</div>
 
<syntaxhighlight lang="Haskell">getLast :: [a] -> a</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">getLast :: [a] -> a</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 76: Line 98:
 
                 | otherwise = getLast' xs
 
                 | otherwise = getLast' xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:6-->
 
* Create a function that checks if an element is a member of the list.
 
* Create a function that checks if an element is a member of the list.
 +
</translate>
 +
 
<syntaxhighlight lang="Haskell">isElement :: Eq a => a -> [a] -> Bool</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">isElement :: Eq a => a -> [a] -> Bool</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 93: Line 120:
 
                   | otherwise = isElement a xs  
 
                   | otherwise = isElement a xs  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:7-->
 
* Create a function that returns the list without the first element.
 
* Create a function that returns the list without the first element.
 +
</translate>
 +
 
<syntaxhighlight lang="Haskell">getTail :: [a] -> [a]</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell">getTail :: [a] -> [a]</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 108: Line 140:
 
getTail (_:xs) = xs
 
getTail (_:xs) = xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:8-->
 
* Create a function that returns the list without the last element.
 
* Create a function that returns the list without the last element.
 +
</translate>
 +
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ZLmvYTZ6fkw]]</div>
 
<syntaxhighlight lang="Haskell">getInit :: [a] -> [a]</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell">getInit :: [a] -> [a]</syntaxhighlight>  
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 124: Line 162:
 
getInit (x:xs) = x : getInit xs
 
getInit (x:xs) = x : getInit xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:9-->
 
* Create a function that merge two lists into one list.
 
* Create a function that merge two lists into one list.
 +
</translate>
 +
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/FFoHC-g3f_g]]</div>
 
<syntaxhighlight lang="Haskell">combine :: [a] -> [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">combine :: [a] -> [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 140: Line 184:
 
combine (x:xs) y = x : combine xs y
 
combine (x:xs) y = x : combine xs y
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:10-->
 
* Create a function that finds the maximum in the list of integers.
 
* Create a function that finds the maximum in the list of integers.
 +
</translate>
 +
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ybgM-YeQTco]]</div>
 
<syntaxhighlight lang="Haskell">max' :: [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">max' :: [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 163: Line 213:
 
                 |otherwise = tmp a xs
 
                 |otherwise = tmp a xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
 +
<translate>
 +
<!--T:11-->
 
* Create a function that reverse a list.
 
* Create a function that reverse a list.
 +
</translate>
 +
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/jlUuZ5fCFgQ]]</div>
 
<syntaxhighlight lang="Haskell">reverse' :: [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">reverse' :: [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 184: Line 240:
 
         tmp (x:xs) ys = tmp xs (x:ys)
 
         tmp (x:xs) ys = tmp xs (x:ys)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
* Create a function that product scalar multiplication if two vectors.
+
<translate>
 +
<!--T:12-->
 +
* Create a function that product scalar multiplication of two vectors.
 +
</translate>
 +
 
 
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
Line 200: Line 261:
 
scalar (x:xs) (y:ys) = x*y + scalar xs ys
 
scalar (x:xs) (y:ys) = x*y + scalar xs ys
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]]
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
 +
 +
<translate>
 +
= Additional exercises = <!--T:13-->
 +
* Create a function that eliminates all occurrences of zeros in a list.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">nonZeros :: [Int] -> [Int]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> nonZeros [0,1,0,2,3,0,0]
 +
[1,2,3]
 +
</syntaxhighlight>
 +
 +
<translate>
 +
<!--T:14-->
 +
* Create a function that realizes the left rotation of a list by one element.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">rotateLeft1 :: [a] -> [a]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> rotateLeft1 [1,2,3,4,5]
 +
[2,3,4,5,1]
 +
</syntaxhighlight>
 +
 +
<translate>
 +
<!--T:15-->
 +
* Create a function that realizes the right rotation of a list by one element.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">rotateRight1 :: [a] -> [a]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> rotateRight1 [1,2,3,4,5]
 +
[5,1,2,3,4]
 +
</syntaxhighlight>
 +
 +
<translate>
 +
<!--T:16-->
 +
* Create a function that eliminates all even numbers from a list.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">oddMembers :: [Int] -> [Int]</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> oddMembers [0,1,0,2,3,0,0]
 +
[1,3]
 +
</syntaxhighlight>
 +
 +
<translate>
 +
<!--T:17-->
 +
* Create a function that counts all odd numbers in a given list (define non-recursively using previously defined functions).
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">countOddMembers :: [Int] -> Int</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> countOddMembers [1,0,3,1,4,5]
 +
4
 +
</syntaxhighlight>
 +
 +
<translate>
 +
<!--T:18-->
 +
* Create a function that compares lists of numbers if they are equal.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">compareLists :: Eq a => [a] -> [a] -> Bool </syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*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
 +
</syntaxhighlight>

Latest revision as of 10:39, 9 October 2023

Usage of lists

Find out the results of the following operations:

[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]

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 first element.
getTail :: [a] -> [a]
*Main> getTail [1,2,3]
[2,3]
getTail :: [a] -> [a]                   
getTail (_:xs) = 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!
  • Create a function that product scalar multiplication of two vectors.
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 if they are equal.
compareLists :: Eq a => [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