Difference between revisions of "FP Laboratory 3"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
== Usage of lists == | == Usage of lists == | ||
+ | <translate> | ||
+ | Find out the results of the following operations: | ||
+ | </translate> | ||
− | |||
<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||
[3,2,1] > [2,1,0] | [3,2,1] > [2,1,0] | ||
Line 10: | Line 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | <translate> | ||
== Simple functions working with list == | == Simple functions working with list == | ||
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 29: | Line 34: | ||
<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> |
+ | * 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 46: | Line 55: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
+ | <translate> | ||
* 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 62: | Line 74: | ||
<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> |
+ | * 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 83: | Line 99: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
+ | <translate> | ||
* 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 101: | Line 120: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
+ | <translate> | ||
* 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 117: | Line 139: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
− | * Create a function that returns the list without the last element. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ZLmvYTZ6fkw]]</div> | + | <translate> |
+ | * 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 134: | Line 160: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
− | * Create a function that merge two lists into one list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/FFoHC-g3f_g]]</div> | + | <translate> |
+ | * 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 151: | Line 181: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
− | * Create a function that finds the maximum in the list of integers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ybgM-YeQTco]]</div> | + | <translate> |
+ | * 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 175: | Line 209: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
− | * Create a function that reverse a list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/jlUuZ5fCFgQ]]</div> | + | <translate> |
+ | * 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 197: | Line 235: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
+ | <translate> | ||
* Create a function that product scalar multiplication if two vectors. | * Create a function that product scalar multiplication if 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"> |
Revision as of 08:19, 19 October 2021
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
- Create a function that sums the list of integers.
sumIt :: [Int] -> Int
*Main> sumIt [1,2,3]
6
- Create a function that returns the first element in the list.
getHead :: [a] -> a
*Main> getHead [1,2,3]
1
- Create a function that returns the last element in the list.
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
- 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
- Create a function that returns the list without the first element.
getTail :: [a] -> [a]
*Main> getTail [1,2,3]
[2,3]
- Create a function that returns the list without the last element.
getInit :: [a] -> [a]
*Main> getInit [1,2,3]
[1,2]
- Create a function that merge two lists into one list.
combine :: [a] -> [a] -> [a]
*Main> combine [1,2,3] [4,5]
[1,2,3,4,5]
- Create a function that finds the maximum in the list of integers.
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
- Create a function that reverse a list.
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)
- Create a function that product scalar multiplication if two vectors.
scalar :: [Int] -> [Int] -> Int
*Main> scalar [1,2,3] [4,5,6]
32