Difference between revisions of "FP Laboratory 3"
Jump to navigation
Jump to search
Line 246: | Line 246: | ||
<translate> | <translate> | ||
<!--T:12--> | <!--T:12--> | ||
− | * Create a function that product scalar multiplication | + | * Create a function that product scalar multiplication of two vectors. |
</translate> | </translate> | ||
Line 268: | Line 268: | ||
* Create a function that eliminates all occurrences of zeros in a list. | * Create a function that eliminates all occurrences of zeros in a list. | ||
− | <syntaxhighlight lang="Haskell"> | + | <syntaxhighlight lang="Haskell"> nonZeros :: [Int] -> [Int]</syntaxhighlight> |
<syntaxhighlight lang="Haskell" class="myDark"> | <syntaxhighlight lang="Haskell" class="myDark"> | ||
− | *Main> | + | *Main> nonZeros [0,1,0,2,3,0,0] |
[1,2,3] | [1,2,3] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 276: | Line 276: | ||
* Create a function that eliminates all even numbers from a list. | * Create a function that eliminates all even numbers from a list. | ||
− | <syntaxhighlight lang="Haskell"> | + | <syntaxhighlight lang="Haskell"> oddMembers :: [Int] -> [Int]</syntaxhighlight> |
<syntaxhighlight lang="Haskell" class="myDark"> | <syntaxhighlight lang="Haskell" class="myDark"> | ||
− | *Main> | + | *Main> oddMembers [0,1,0,2,3,0,0] |
[1,3] | [1,3] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 284: | Line 284: | ||
* Create a function that counts all odd numbers in a given list (define non-recursively using previously defined functions). | * Create a function that counts all odd numbers in a given list (define non-recursively using previously defined functions). | ||
− | <syntaxhighlight lang="Haskell"> | + | <syntaxhighlight lang="Haskell"> countOddMembers :: [Int] -> Int</syntaxhighlight> |
<syntaxhighlight lang="Haskell" class="myDark"> | <syntaxhighlight lang="Haskell" class="myDark"> | ||
− | *Main> | + | *Main> countOddMembers [1,0,3,1,4,5] |
4 | 4 | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 13:19, 3 October 2022
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 of two vectors.
scalar :: [Int] -> [Int] -> Int
*Main> scalar [1,2,3] [4,5,6]
32
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 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